In order to retrieve some information of the target machine, I am using following code:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
string collectedInfo = ""; // here we will put the informa
foreach (ManagementObject share in searcher.Get())
{
// first of all, the processorid
collectedInfo += share.GetPropertyValue("ProcessorId").ToString ();
}
searcher.Query = new ObjectQuery("select * from Win32_BIOS");
foreach (ManagementObject share in searcher.Get())
{
//then, the serial number of BIOS
collectedInfo +=share.GetPropertyValue("SerialNumber").ToString ();
}
searcher.Query = new ObjectQuery("select * from Win32_BaseBoard");
foreach (ManagementObject share in searcher.Get())
{
//finally, the serial number of motherboard
collectedInfo+= share.GetPropertyValue("SerialNumber").ToString();
}
This works fine while it is executed as a asp.net website on my local computer, but as I publish it on another web server, it shows a different number.
Is there a way to make it calculate the users information, instead of calculating it for a server?
Related
I can't get DateTime from InstallDate - CPU, GPU, RAM, HDD. Always appears an error System.NullReferenceException, or datetime like 01.01.0001 00.00.00
This is my code with 2 expample:
DateTime CpuDateTime, GpuDateTime; and etc.
ManagementObjectSearcher mos =
new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
foreach (ManagementObject mo in mos.Get())
{
CpuDateTime = ManagementDateTimeConverter.ToDateTime(mo["InstallDate"].ToString());
}
ManagementObjectSearcher video =
new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_VideoController");
foreach (ManagementObject mo in video.Get())
{
GpuDateTime = ManagementDateTimeConverter.ToDateTime(mo["InstallDate"].ToString());
}
CpuTimeLabel.Text = CpuDateTime.ToString();
GpuTimeLabel.Text = GpuDateTime.ToString();
if i use this code : ManagementDateTimeConverter.ToDateTime(mo["InstallDate"].ToString()); i take this error - System.NullReferenceException
if i use this code without .ToString: ManagementDateTimeConverter.ToDateTime(mo["InstallDate"]); I take like this 01.01.0001 00.00.00
Please Help. Thank you very much
is there any way to check if printer supports postscript, using C#? I need to check this before I do anything with my document.
Thanks,
Bartosz
You could use WMI potentially, however im not sure if this solution will be reliable
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos = new ManagementObjectSearcher(oq);
ManagementObjectCollection moc = mos.Get();
foreach( ManagementObject mo in moc )
{
string name = mo["Name"].ToString();
string language = mo["DefaultLanguage"].ToString();
MessageBox.Show(String.Format("Printer: {0} -- Language: {1}", name, language));
}
Lifted from here
Update
Check here to see other fields that might be relevant
Win32_Printer class
In particular uint16 LanguagesSupported[];
Code, which I've finally used, with little changes:
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos = new ManagementObjectSearcher(oq);
ManagementObjectCollection moc = mos.Get();
foreach (ManagementObject mo in moc)
{
string name = mo["Name"].ToString();
var language = mo["LanguagesSupported"];
Console.WriteLine(String.Format("Printer: {0} -- Language: {1}", name, language==null ? 0 : (language as ushort[])[0]));
}
i need service thats starts automatically on the Windows Server.
Can someone show me a way to get the Status of the Windows Server (Online or Offline) using WMI.
Thank you in advance
Please try below code,
string FullComputerName = "<Name of Remote Computer>";
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + FullComputerName + "\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalService");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject queryObj in queryCollection)
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_TerminalService instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Started: {0}", queryObj["Started"]);
Console.WriteLine("State: {0}", queryObj["State"]);
Console.WriteLine("Status: {0}", queryObj["Status"]);
}
Source :- https://social.msdn.microsoft.com/Forums/vstudio/en-US/a25d3071-2283-41c6-9262-6860d7965963/how-to-check-remote-servers-terminal-status-using-c?forum=csharpgeneral
I'm trying to invert the following query:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = 4856")) {
foreach (ManagementObject mo in searcher.Get()) {
Debug.WriteLine(mo["CommandLine"]);
}
}
Which returns the expected result:
C:\Windows\Explorer.EXE
INTO:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT ProcessId FROM Win32_Process WHERE CommandLine = 'C:\\Windows\\Explorer.EXE'")) {
foreach (ManagementObject mo in searcher.Get()) {
Debug.WriteLine(mo["ProcessId"]);
}
}
Which throws an Invalid query exception and not the process id.
Ok, I just figured it out. I have to double up the slashes and escape characters in the path in the query.
Both the C# compiler and the WMI SQL implementation wants escaped slashes i guess. Stupid computers.
SELECT ProcessId FROM Win32_Process WHERE CommandLine = 'C:\\\\windows\\\\explorer.EXE'
If there are 3 pcs on network and if I want to detect the freespace and disk details of one pc from another pc then how to go about it...
I have found this code. but I don't know how should I test it inorder to know whether it is working.
is this the right way?
public Hashtable ReadFreeSpaceOnNetworkDrives()
{
//create Hashtable instance to hold our info
Hashtable driveInfo = new Hashtable();
//query the win32_logicaldisk for type 4 (Network drive)
SelectQuery query = new SelectQuery("select name, FreeSpace from win32_logicaldisk where drivetype=4");
//execute the query using WMI
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
//loop through each drive found
foreach (ManagementObject drive in searcher.Get())
{
//add the name & freespace to our hashtable
driveInfo.Add("Drive", drive["name"]);
driveInfo.Add("Space", drive["FreeSpace"]);
}
return driveInfo;
}
Update:
I got the answer to my question but I have got the code but it is in console application and I want in windows form application with a graphical representation of the disk space and drive info. How can I use this code and go about doing that?
ManagementScope scope = new ManagementScope("\\\\10.74.160.126\\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();
}
Console.ReadLine();
}
The code checks all the drives on the pc where you run this program. It returns a table with 2 entries per drive. One with the name and one with the free space. You can just write a simple program that uses this method and displays this data. It should be possible to query the drives from a remote computer. Maybe this article can tell you more http://msdn.microsoft.com/en-us/library/ms257337%28v=vs.80%29.aspx
EDIT:
public Hashtable ReadFreeSpaceOnNetworkDrives(String FullComputerName)
{
ManagementScope scope = new ManagementScope(fullComputerName);
scope.Connect();
//create Hashtable instance to hold our info
Hashtable driveInfo = new Hashtable();
//query the win32_logicaldisk for type 4 (Network drive)
SelectQuery query = new SelectQuery("select name, FreeSpace from win32_logicaldisk where drivetype=4");
//execute the query using WMI
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,query);
//loop through each drive found
foreach (ManagementObject drive in searcher.Get())
{
//add the name & freespace to our hashtable
driveInfo.Add("Drive", drive["name"]);
driveInfo.Add("Space", drive["FreeSpace"]);
}
return driveInfo;
}
covert into c#. and that may help you. http://www.codeguru.com/forum/showthread.php?t=426869