As the title says, is there a way to get the total (installed) system RAM in .NET Standard 2.0? All my searches only turn up with .NET Framework solutions.
You can use System.Management with NET Standard 2.0. Hope this can help you:
using System;
using System.Diagnostics;
using System.Management;
namespace RamSystem
{
public class RamSystem
{
public static void GetRAM()
{
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
double res;
foreach (ManagementObject result in results)
{
res = Convert.ToDouble(result["TotalVisibleMemorySize"]);
double fres = Math.Round((res / (1024 * 1024)), 2);
Console.WriteLine("Total usable memory size: " + fres + "GB");
Console.WriteLine("Total usable memory size: " + res + "KB");
}
Console.ReadLine();
}
}
}
Related
I am not a C# programer but I need to format drive with 32KB cluster using C#. I found "Format method of the Win32_Volume class" but when I am trying to format drive I always get an error 15 (Cluster size is too small). This is my code:
public static int DriveFormatting(string driveLetter)
{
string FileSystem = "FAT32", Label = "";
Boolean QuickFormat = true, EnableCompression = false;
UInt32 ClusterSize = 32;
int result = -1;
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Volume WHERE DriveLetter = '"+ driveLetter +"'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementObject management in searcher.Get())
{
Console.WriteLine("Formating disk " + driveLetter + "...");
result = Convert.ToInt32(management.InvokeMethod("Format", new object[] { FileSystem, QuickFormat, ClusterSize, Label, EnableCompression }));
}
return result;
}
How can I do this? Thanks in advance.
I'm currently making a program the monitors the uptime and size of servers on a network. I've run into some problems with displaying the size of the servers and what space is left.
My code is
public void setSpace(string ip)
{
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection1 = searcher1.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Computer Name : {0}",
m["csname"]);
Console.WriteLine("Windows Directory : {0}",
m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}",
m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
Console.WriteLine();
}
foreach (ManagementObject mo in queryCollection1)
{
Console.WriteLine(" Disk Name : {0}", mo["Name"]);
Console.WriteLine(" Disk Size : {0}", mo["Size"]);
Console.WriteLine(" FreeSpace : {0}", mo["FreeSpace"]);
Console.WriteLine(" Disk DeviceID : {0}", mo["DeviceID"]);
Console.WriteLine(" Disk VolumeName : {0}", mo["VolumeName"]);
Console.WriteLine(" Disk SystemName : {0}", mo["SystemName"]);
Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
Console.WriteLine();
freeSpace = freeSpace + (double)mo["FreeSpace"];
totalSpace = totalSpace + (double)mo["Size"];
}
Console.ReadLine();
}
I'm trying to take the FreeSpace and Size numbers and put them in a double variable.
freeSpace = freeSpace + (double)mo["FreeSpace"];
totalSpace = totalSpace + (double)mo["Size"];
When I try to run the program I get an exception.
'Object reference not set to an instance of an object.'
You got a NullReferenceException because the code doesn't return a specified member (eg. FreeSpace). I tried the code in my PC where I have 2 partitions and 1 DVD drive. Exception was raised when it reached the DVD drive (empty disk) while it was iterating the queryCollection1 object. So you must check if the member of mo object you want is a null reference.
if (mo["FreeSpace"] != null)
freeSpace = freeSpace + (ulong)mo["FreeSpace"];
if (mo["Size"] != null)
totalSpace = totalSpace + (ulong)mo["Size"];
I also change the cast to ulong because FreeSpace and Size are ulong type. And there's an implicit conversion from ulong to double. You don't need to change the variable type of freeSpace and totalSpace.
Your casts are wrong. Instead of cast to double, you should cast to ulong. The variables freeSpace and totalSpace should be of the same type and initialized before the first use.
freeSpace = freeSpace + (ulong)mo["FreeSpace"];
totalSpace = totalSpace + (ulong)mo["Size"];
today i wrote my second Code in C#
why wont it work? the code ist (as it seems) correct!
error code: InvalidQuery
Code:
static void Main(string[] args)
{
GetComponent("Win32_Processor", "Name");
Console.Read();
Console.ReadKey();
}
private static void GetComponent(string hwclass, string syntax)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher ("root\\CIMV2","SELECT * FROM" + hwclass);
foreach(ManagementObject mj in mos.Get())
{
Console.WriteLine(Convert.ToString(mj[syntax]));
}
}
Please, use formatting or string interpolation (C# 6.0+) to avoid syntax errors:
private static void GetComponent(string hwclass, string syntax) {
//DONE: keep query readable
string query =
$#"select *
from {hwclass}"; // <- you've missed space here
//DONE: wrap IDisposable into using
using (ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", query)) {
foreach(ManagementObject mj in mos.Get())
Console.WriteLine(Convert.ToString(mj[syntax]));
}
}
You are missing a space after the "FROM":
("root\\CIMV2","SELECT * FROM" + hwclass);
Change to:
("root\\CIMV2","SELECT * FROM " + hwclass);
I couldnt able to find the cached and free memory of a system using C#.Help me.......
Add Microsoft.VisualBasic.Devices assembly reference to your project then you can use following
var Available = new ComputerInfo().AvailablePhysicalMemory;
var Total = new ComputerInfo().TotalPhysicalMemory;
var Cheched = Total - Available;
Edit:
Following code works for me, also note that Available amount includes the Free Amount and also includes most of the Cached amount.
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
//total amount of free physical memory in bytes
var Available = new ComputerInfo().AvailablePhysicalMemory;
//total amount of physical memory in bytes
var Total = new ComputerInfo().TotalPhysicalMemory;
var PhysicalMemoryInUse = Total - Available;
Object Free = new object();
foreach (var result in results)
{
//Free amount
Free = result["FreePhysicalMemory"];
}
var Cached = Total - PhysicalMemoryInUse - UInt64.Parse(Free.ToString());
Console.WriteLine("Available: " + ByteToGb(Available));
Console.WriteLine("Total: " + ByteToGb(Total));
Console.WriteLine("PhysicalMemoryInUse: " + ByteToGb(PhysicalMemoryInUse));
Console.WriteLine("Free: " + ByteToGb(UInt64.Parse( Free.ToString())));
Console.WriteLine("Cached: " + ByteToGb(Cached));
I need a way to check available disk space on a remote Windows server before copying files to that server. Using this method I can check to see if the primary server is full and if it is, then I'll copy the files to a secondary server.
How can I check for available disk space using C#/ASP.net 2.0?
You can check it by doing the following:
Add the System.Management.dll as a reference to your project.
Use the following code to get the diskspace:
using System;
using System.Management;
public string GetFreeSpace();
{
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
string freespace = disk["FreeSpace"];
return freespace;
}
There are a myriad of ways to do it, I'd check the System.Management namespace for more ways.
Here's one such way from that page:
public void GetDiskspace()
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\localhost\\root\\cimv2",
options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection1 = searcher1.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Computer Name : {0}", m["csname"]);
Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}", m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
Console.WriteLine();
}
foreach (ManagementObject mo in queryCollection1)
{
// Display Logical Disks information
Console.WriteLine(" Disk Name : {0}", mo["Name"]);
Console.WriteLine(" Disk Size : {0}", mo["Size"]);
Console.WriteLine(" FreeSpace : {0}", mo["FreeSpace"]);
Console.WriteLine(" Disk DeviceID : {0}", mo["DeviceID"]);
Console.WriteLine(" Disk VolumeName : {0}", mo["VolumeName"]);
Console.WriteLine(" Disk SystemName : {0}", mo["SystemName"]);
Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
Console.WriteLine();
}
string line;
line = Console.ReadLine();
}
by using this code
static void Main()
{
try
{
DriveInfo driveInfo = new DriveInfo(#"C:");
long FreeSpace = driveInfo.AvailableFreeSpace;
}
catch (System.IO.IOException errorMesage)
{
Console.WriteLine(errorMesage);
}
}
IF you are getting the error 'The device is not ready' .i.e your device is not ready .
If you are trying this code for a CD drive without CD you will get the same error : )
This seems to be an option from the System.IO:
DriveInfo c = new DriveInfo("C");
long cAvailableSpace = c.AvailableFreeSpace;
You can use the DriveInfo class
DriveInfo[] oDrvs = DriveInfo.GetDrives();
foreach (var Drv in oDrvs) {
if (Drv.IsReady) {
Console.WriteLine(Drv.Name + " " + Drv.AvailableFreeSpace.ToString);
}
}
You can use WMI, see this related question:
Best way to query disk space on remote server