I'm trying to duplicate the exact file listing below which is found under "Driver Details" in Device Manager, i've come up with the below code but i'm unable to display this list. Any help is awesome.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPSignedDriver");
foreach (ManagementObject obj in searcher.Get())
{
if(obj["DriverProviderName"] != null)
foreach (PropertyData prop in obj.Properties)
{
File.AppendAllText(#"C:\driverusers.txt", prop.Name +"\t" +prop.Value+"\n");
Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
}
}
Take a look at this, someone was doing something very similar.
http://www.dreamincode.net/forums/topic/63149-devices-in-c%23/
The first thing I can think of is to go hunt for it in the registry. (I think they even state this in the article above).
Also, check this out:
http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I
Related
I'm trying to return, or in this example print out each user that has been created on a windows machine, I don't really care about the windows users like "defaultuser0", but if anyone can tell me how I can exclude them aswell, that would be great, but back to the main question.
I have this code below, and it works to a certain degree. Output I received was this.
\PC_NAME\root\cimv2:Win32_UserAccount.Domain="PC_NAME",Name="admin"
\PC_NAME\root\cimv2:Win32_UserAccount.Domain="PC_NAME",Name="Administrator"
\PC_NAME\root\cimv2:Win32_UserAccount.Domain="PC_NAME",Name="DefaultAccount"
\PC_NAME\root\cimv2:Win32_UserAccount.Domain="PC_NAME",Name="defaultuser0"
\PC_NAME\root\cimv2:Win32_UserAccount.Domain="PC_NAME",Name="Guest"
\PC_NAME\root\cimv2:Win32_UserAccount.Domain="HEAVEN",Name="WDAGUtilityAccount"
Now I understand that this is natural behaviour of my code. Is there a way I can get the actual usernames, without including the domain and all the other directory code?
Here is the code I'm using.
var searcher = new ManagementObjectSearcher(new SelectQuery("Win32_UserAccount"));
foreach (var managementBaseObject in searcher.Get())
{
Console.WriteLine(((ManagementObject) managementBaseObject).ToString());
}
Instead of letting the compiler choose the base type ManagementBaseObject, you can specify that you only want all ManagementObjectinstances in the collection.
foreach (ManagementObject instance in searcher.Get())
This way you do not need to cast explicitely again.
To access any property of such a ManagementObject use the [string] access notation. The string has to be the property name, in your case it would be Name.
Console.WriteLine("Username: {0}", instance["Name"]);
The full code would be:
var searcher = new ManagementObjectSearcher(new SelectQuery("Win32_UserAccount"));
foreach (ManagementObject instance in searcher.Get())
{
var strUsername = instance["Name"];
Console.WriteLine("Username: {0}", strUsername);
}
I used this code for finding graphic cards:
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
string graphicsCard = "";
foreach (ManagementObject mo in searcher.Get())
{
foreach (PropertyData property in mo.Properties)
{
if (property.Name == "Description")
{
graphicsCard += property.Value.ToString();
}
}
}
but result is: Nvidia Quadro K6000
How to find all Graphic cards?
The very first line of the MSDN page reads:
[The Win32_DisplayConfiguration WMI class is no longer available for use as of Windows Server 2008. Instead, use the properties in the Win32_VideoController, Win32_DesktopMonitor, and CIM_VideoControllerResolution classes.]
So I suggest you start out with Win32_VideoController.
I have a C# project where I am trying to deploy a protection mechanism by registering a combination of the Hardware IDs.
I am using the ManagementObjectSearcher Class for the same. Here are some of the commands:
ManagementObjectSearcher cpuget = new ManagementObjectSearcher("Select * From Win32_processor");
ManagementObjectSearcher mainboardget = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectSearcher biosget = new ManagementObjectSearcher("Select * From Win32_BIOS");
For fetching the IDs I have:
foreach (ManagementObject mo in cpuList)
{
cpuid = mo["ProcessorID"].ToString();
}
foreach (ManagementObject mo in mainboardlist)
{
mbid = mo["SerialNumber"].ToString();
}
This has been working fine. However, *in some machines(I tested on 10 PCs and two were defaulters)* the error message showed up.
Reference not set to Instance of an Object
Why so? Please Help.
The most difficult problems have the silliest of Answers. Period.
All I had to do was Check for a null component in the management object class.
foreach (ManagementObject mo in cpuget.Get())
{
if (mo["ProcessorID"] != null)
cpuid = mo.GetPropertyValue("ProcessorID").ToString();
}
The similar would be for mbid.
What a dufus? :|
P.S.: #L.B suggested this. More power to him.
I need to enumerate all user profiles on a local computer and list them in a combo box. Any special accounts need to be filtered out. I'm only concerned about actual user profiles on the computer where the app is running. I have done some searching but I haven't found a clear answer posted anywhere. I did find some code that might work but SelectQuery and ManagementObjectSearcher are displaying errors in VS and I'm not sure what I need to do to make this work.
using System.Management;
SelectQuery query = new SelectQuery("Win32_UserAccount");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject envVar in searcher.Get())
{
Console.WriteLine("Username : {0}", envVar["Name"]);
}
By saying "SelectQuery and ManagementObjectSearcher are displaying errors" I guess you didn't reference the System.Management dll.
You should right click References in your solution and add System.Management.
Then, with your using statement, the errors should disappear.
Anyway, including the error next time will assist everyone to help you :)
The mentioned code is great but when I tried on a machine connected to a Active Directory Domain all the usernames where returned for the domain. I was able to tweak the code a bit to only return the users that actually have a local directory on the current machine. If a better C# developer can refactor the code to make it cleaner - please help!
var localDrives = Environment.GetLogicalDrives();
var localUsers = new List<string>();
var query = new SelectQuery("Win32_UserAccount") { Condition = "SIDType = 1 AND AccountType = 512" };
var searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject envVar in searcher.Get())
{
foreach (string drive in localDrives)
{
var dir = Path.Combine(String.Format("{0}Users", drive), envVar["name"].ToString());
if (Directory.Exists(dir))
{
localUsers.Add(envVar["name"].ToString());
}
}
}
Once you have the localUsers variable you can set this as the data source to your ComboBox control of our choice.
I'm trying to recognize drives types by looping around DriveInfo.GetDrives() result.
But for both USB and floppy I get the same DriveType.Removable value.
How can I distinguish between them?
You can use WMI (Windows Management Instrumentation) to get more than just what's in the DriveInfo class. In this case, you can get the interface type, which will be "USB" for USB drives.
Sample code is below. You need to add a reference to System.Management.
using System.Management;
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_DiskDrive");
foreach(ManagementObject queryObj in searcher.Get())
{
foreach(ManagementObject o in queryObj.GetRelated("Win32_DiskPartition"))
{
foreach(ManagementBaseObject b in o.GetRelated("Win32_LogicalDisk"))
{
Debug.WriteLine(" #Name: {0}", b["Name"]);
}
}
// One of: USB, IDE
Debug.WriteLine("Interface: {0}", queryObj["InterfaceType"]);
Debug.WriteLine("--------------------------------------------");
}
}
catch (ManagementException f)
{
Debug.WriteLine(f.StackTrace);
}
For reference, this MSDN page documents the full list of accessible properties (since you don't get autocomplete on this).
The CD Drive And floppy Drive is not ready so you can try this :
foreach (var dr in DriveInfo.GetDrives())
{
if (dr.IsReady == true)
{
Console.WriteLine(string.Format("name : {0} type : {1}", dr, dr.DriveType));
}
}
This is Easy Way to distinguish Between USB and floppy devices