searching for installed drivers: all properties are empty - c#

I want to get all a list of all installed drivers on my machine. For this purpose I use WMI. But for unknown reason all properties returned have empty values (except for deviceID, DriverVersion and Signer) .
What I tried and considered so far:
running with and without admin rights makes no difference
no methods found like fetch, load, reload on any of involved objects found
no option found like lazyload
pnputil.exe and driversearch.exe do return data of all drivers, but the outputs of those a very hard to interpret
This is the C#-code:
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPSignedDriver"))
{
foreach (ManagementBaseObject u in searcher.Get())
{
foreach(PropertyData prop in u.Properties)
{
System.Console.WriteLine($"{prop.Name}={prop.Value}");
}
System.Console.WriteLine("===");
}
}
Output for first driver found:
Caption=
ClassGuid=
CompatID=
CreationClassName=
Description=
DeviceClass=
DeviceID=\\SVWPSW46\P3115 X WC 7120-D C S U A3
DeviceName=
DevLoader=
DriverDate=
DriverName=
DriverProviderName=
DriverVersion=2:6.0,2:5.2,2:5.1,2:5.0
FriendlyName=
HardWareID=
InfName=
InstallDate=
IsSigned=True
Location=
Manufacturer=
Name=
PDO=
Signer=Microsoft Windows
Started=
StartMode=
Status=
SystemCreationClassName=
SystemName=
===
...

Related

Retrieve Hard drive info through WMI .NET C#

So i've used this code to get S.M.A.R.T. hard drive diagnostics info from Windows WMI:
http://vasters.com/archive/Reading-ATAPI-SMART-Data-From-Drives-Using-NET-Temperature-Anyone.html
It works on most computers and doesn't on others. After a little bit research and sample testing i found out that it doesn't work on computers whose Hard drives are connected with PCI Express instead of SATA.
var searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData");
foreach (ManagementObject queryObj in searcher.Get())
{
//do stuff
}
This is where i get exception:
System.Management.ManagementException: 'Not supported'
Is there any other way to do this, or add something in this code to make it work?
I searched this code everywhere and couldn't find working for me, but finaly found on another forum so it's really simple in usage.
Just from my example another way to get WMI info. More Attributes of WMI Win32_DiskDrive you can find in Microsoft DOCs
https://github.com/Mityugin/greentest
string NamespacePath = "\\\\.\\ROOT\\cimv2";
string ClassName = "Win32_DiskDrive";
oClass = new ManagementClass(NamespacePath + ":" + ClassName);
foreach (ManagementObject oObject in oClass.GetInstances())
{
var sign = Convert.ToString(oObject["Signature"]);
var model = Convert.ToString(oObject["Model"]);
var status = Convert.ToString(oObject["Status"]);
if (Equals(sign,""))
{
TextBox1.AppendText("DISK model: " + model);
TextBox1.AppendText(Environment.NewLine);
TextBox1.AppendText("Status: " + status);
TextBox1.AppendText(Environment.NewLine);
if (!status.Equals("OK") || !model.Contains("SSD"))
{
//Here is info if Disk is not OK or model not SSD
}
}
}
This github solution is fine for detecting an SSD but will not tell you it is PCIE or NVMe. I have an SSD that is an older Samsung 960 series and it is an SSD connected via SATA.
The code you need to read an NVMe drive Temperature is on this website at How do I enumerate NVMe (M2) drives an get their temperature in c#?

Try to read Values in LOcal Group policy

I am trying to read two setting values in Local Group policy (gpedit.msc). The path is :
Local Computer Policy\Windows Settings\Security Settings\Local Policies\User Rights Assignment
The Policy that I want to read are :
1. Perform volume maintainace tasks (Users assigned to it)
2. Lock Pages in memory (Users assigned to it).
I have searched the web (including all stackoverflow threads) and could not find a solution to this but could not get a solution to this. Below is the code I am using currently but it only returns me 7 values.
I am not sure if this is possible. Please suggest. I am using C# .NET as language and would prefer if possible be able to read these setting from a remote machine (so I am preferring WMI approach).
Also I only want to read values. Now editing or writing...
Please suggest..
Girija
Code
private void Test()
{
ManagementScope scope =
new ManagementScope(
"\\\\localhost\\root\\rsop\\Computer");
scope.Connect();
ObjectQuery query = new ObjectQuery(
"SELECT * FROM RSOP_UserPrivilegeRight");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
List<string> val = new List<string>();
foreach (ManagementObject mgo in queryCollection)
{
var d = mgo["Name"];
val.Add(Convert.ToString(d));
}
}

How to access BIOS information from a Windows 8 app?

I know we can get the BIOS information using system.management assembly but the assembly is not accessible for windows 8 app. I specifically need to know the serial number of the laptop on which the app is running. Is there any way that I can access that ?
I don't think there is a way if you are developing a Windows Modern UI App.
Modern UI Apps get run in a sandbox environment which have very limited access to anything. Check MSDN documentations on that.
If you are developing a desktop Windows app on the other hand, then try the following code:
(You need to import System.Management.dll into your project.)
using System;
using System.IO;
using System.Management;
namespace GetHardwareIds
{
internal class Program
{
private static void Main(string[] args)
{
using (StreamWriter writer = new StreamWriter(#"C:\HardwareInfo.txt"))
{
using
(
ManagementObjectSearcher searcher =
// Where __Superclass Is Null: selects only top-level classes.
// remove it if you need a list of all classes
// new ManagementObjectSearcher("Select * From meta_class Where __Superclass Is Null")
// this query only select the processor info. for more options uncomment top line
new ManagementObjectSearcher("Select * From meta_class Where __Class = 'Win32_Processor'")
)
{
foreach (ManagementObject managementObject in searcher.Get())
{
Console.WriteLine(managementObject.Path.ClassName);
writer.WriteLine(managementObject.Path.ClassName);
GetManagementClassProperties(managementObject.Path.ClassName, writer);
managementObject.Dispose();
}
}
}
}
public static void GetManagementClassProperties(string path, StreamWriter writer)
{
using (ManagementClass managementClass = new ManagementClass(path))
{
foreach (ManagementObject instance in managementClass.GetInstances())
{
foreach (PropertyData property in instance.Properties)
{
Console.WriteLine(" {0} = {1}", property.Name, property.Value);
writer.WriteLine(" {0} = {1}", property.Name, property.Value);
}
instance.Dispose();
}
}
}
}
}
Check this code. I am not a 100% clear on what you are trying to achieve but this code should return the device ID specified by Win8 (this code includes a concatenation of all ids.)
// get hardware token
HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
// get hardware ID bytes
byte[] idBytes = hwToken.Id.ToArray();
// populate device ID as a string value
string deviceID = string.Join(",", idBytes);
Here is the link to MSDN articles about it:
http://msdn.microsoft.com/en-us/library/windows/apps/jj553431.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.profile.hardwareidentification.getpackagespecifictoken.aspxThere is an entry for BIOS in the return structure based on these articles.
Hopefully, this does what you need. Let me know if it worked :)
Unfortunately the information you want to obtain is not available to WinRT applications.

ManagementObjectSearcher error

Some of our customers inform us that in some cases following error appears:
System.Management.ManagementException: Błąd dostawcy.
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)
at
System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
The error is generated while trying to loop through a colection returned by Get() method of the System.Mamangment.ManagementObjectSearcher object.
This is the code of my method:
private bool PrinterExists(string printerName)
{
bool retVal = false;
SelectQuery q = new SelectQuery("select caption from win32_printer");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(q))
{
foreach (ManagementObject printer in searcher.Get())
{
if(printer["Caption"].ToString() == printerName)
{
retVal = true;
break;
}
}
}
return retVal;
}
It seems that the problem appears only on Windows XP. The only workaround I know is reconstruction of WMI database. It sometimes helps, but unfortunatelly not always.
Can anyone tell me what is the reason of this error and how can I fix it?
There a many possible reasons you could get an error while enumerating a WMI collection, including that you don't have permissions to look at some properties of an object. You can check the permissions possibility by running the app as administrator and seeing if the error goes away.
Regardless of the underlying root cause, one solution you could try is to modify your WQL query to include the name of the printer you're looking for. By having WMI do the enumeration instead of you, it might bypass the problematic items.
SelectQuery q = new SelectQuery(
"select caption from win32_printer where Caption='Fax' ");
bool found = new System.Management.ManagementObjectSearcher(q).Get().Count > 0;
If that doesn't work, then put your comparison of Caption into an exception handler, and ignore printers that throw exceptions.
Of course, if the underlying problem is that you're trying to find printers that you don't have permissions to look at, then you'll need to adjust your application so that it's running with elevated permissions.

different results for web and desktop applications for same code

I have the following piece of code.
It is returning different results when running on the same machine in case of web and desktop applications.
Here is my code. Please guide me on what to do regarding this???
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
return (from ManagementObject wmiHD in searcher.Get()
select wmiHD["SerialNumber"] == null ? "VM HD" : wmiHD["SerialNumber"].ToString()).ToList();
Here is a LINQ-free version of the same code
var hdCollection = new List<string>();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject wmiHD in searcher.Get())
{
// get the hardware serial no.
if (wmiHD["SerialNumber"] == null)
{
hdCollection.Add("VM HD");
}
else
{
hdCollection.Add(wmiHD["SerialNumber"].ToString());
}
}
return hdCollection;
That could possibly be caused by two things:
web server runs with different user account (probably NetworkService)
http://www.bluevisionsoftware.com/WebSite/TipsAndTricksDetails.aspx?Name=AspNetAccount
web server runs code without Fulltrust permissions (probably medium trust)
http://discussion.accuwebhosting.com/iis-web-server/993-how-grant-full-trust-mode-domain-asp-net-2-0-iis-6-0-a.html
Both actions can compromise security, but the first one gives more choices to fix this by setting ACLs.

Categories

Resources