In my application i want to see if windows 7 is activated.
To be clear, i do not want to check if windows are genuine.
I use the code below, found here http://www.dreamincode.net/forums/topic/166690-wmi-softwarelicensingproduct/
The time needed to execute the query is about 5-10 sec. Is there anyway to reduce the time needed? Or another way to check if winows 7 is activated?
public string VistaOrNewerStatus(){
string status = string.Empty;
string computer = ".";
try
{
//set the scope of this search
ManagementScope scope = new ManagementScope(#"\\" + computer + #"\root\cimv2");
//connect to the machine
scope.Connect();
//use a SelectQuery to tell what we're searching in
SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct");
//set the search up
ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);
//get the results into a collection
using (ManagementObjectCollection obj = searcherObj.Get())
{
MessageBox.Show(obj.Count.ToString());
//now loop through the collection looking for
//an activation status
foreach (ManagementObject o in obj)
{
//MessageBox.Show(o["ActivationRequired"].ToString());
switch ((UInt32)o["LicenseStatus"])
{
case 0:
status = "Unlicensed";
break;
case 1:
status = "Licensed";
break;
case 2:
status = "Out-Of-Box Grace Period";
break;
case 3:
status = "Out-Of-Tolerance Grace Period";
break;
case 4:
status = "Non-Genuine Grace Period";
break;
}
}
}
// return activated;
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString());
status = ex.Message;
//return false;
}
return status;
}
I would recommend querying only the properties you really need. So, if you only need the LicenseStatus value of the SoftwareLicensingProduct WMI class then use the following query:
SelectQuery searchQuery = new
SelectQuery("SELECT LicenseStatus FROM SoftwareLicensingProduct");
This should improve your performance. As DJ KRAZE pointed out in his answer you should of course dispose your management classes.
On my Windows 7 machine using only the LicenseStatus property in the query took 246ms. Querying for all properties (using the "*") took 2440ms.
This is generally the way WMI works it's querying at least.. where you have the following below .. after your foreach I would dispose of those objects ..
ManagementScope scope = new ManagementScope(#"\\" + computer + #"\root\cimv2");
//connect to the machine
scope.Connect();
//use a SelectQuery to tell what we're searching in
SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct");
//set the search up
ManagementObjectSearcher searcherObj
if they implement IDisposeable then you could do
((IDisposable)scope).Dispose();
((IDisposable)searchQuery).Dispose();
((IDisposable)searcherObj).Dispose();
if not then do an if() to check if the object != null then Dispose of them individually
Try running this several times and seeing if it returns faster or not once you dispose of the objects.. other than that.. not much you can do from what it looks like to make it faster..
This i made is quick :)
public bool IsLicensed(bool Licensed = false)
{
try
{
foreach (ManagementObject Obj in new ManagementObjectSearcher("root\\CIMV2", "SELECT LicenseStatus FROM SoftwareLicensingProduct WHERE LicenseStatus = 1").Get())
{
Licensed = true;
}
}
catch (ManagementException) { Licensed = false; }
return Licensed;
}
Its usage:
if(IsLicenced())
MessageBox.Show("Windows is Licensed");
Related
I tried many things:
//public static string GetMotherBoardID()
//{
// string mbInfo = String.Empty;
// //Get motherboard's serial number
// ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
// foreach (ManagementObject mo in mbs.Get())
// mbInfo += mo["SerialNumber"].ToString();
// return mbInfo;
//}
//public static string GetMotherBoardID()
//{
// string mbInfo = String.Empty;
// ManagementScope scope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
// scope.Connect();
// ManagementObject wmiClass = new ManagementObject(scope, new ManagementPath("Win32_BaseBoard.Tag=\"Base Board\""), new ObjectGetOptions());
// foreach (PropertyData propData in wmiClass.Properties)
// {
// if (propData.Name == "SerialNumber")
// mbInfo = String.Format("{0,-25}{1}", propData.Name, Convert.ToString(propData.Value));
// }
// return mbInfo;
//}
public static string GetMotherBoardID()
{
string mbInfo = String.Empty;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
ManagementObjectCollection moc = mbs.Get();
ManagementObjectCollection.ManagementObjectEnumerator itr = moc.GetEnumerator();
itr.MoveNext();
mbInfo = itr.Current.Properties["SerialNumber"].Value.ToString();
var enumerator = itr.Current.Properties.GetEnumerator();
if (string.IsNullOrEmpty(mbInfo))
mbInfo = "0";
return mbInfo;
}
This all gives empty string on my PC, but the correct ID on the laptop.
Some other person also reporting on two PCs is empty motherboard ID.
The result of:
public static string GetMotherBoardID()
{
string mbInfo = String.Empty;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
ManagementObjectCollection moc = mbs.Get();
ManagementObjectCollection.ManagementObjectEnumerator itr = moc.GetEnumerator();
itr.MoveNext();
mbInfo = itr.Current.Properties["SerialNumber"].Value.ToString();
var enumerator = itr.Current.Properties.GetEnumerator();
string properties = "";
while (enumerator.MoveNext())
{
properties += "[" + enumerator.Current.Name + "][" + (enumerator.Current.Value != null ? enumerator.Current.Value.ToString() : "NULL") + "]\n";
}
if (string.IsNullOrEmpty(mbInfo))
mbInfo = "0";
return mbInfo;
}
[Caption][Основная плата]
[ConfigOptions][NULL]
[CreationClassName][Win32_BaseBoard]
[Depth][NULL]
[Description][Основная плата]
[Height][NULL]
[HostingBoard][True]
[HotSwappable][False]
[InstallDate][NULL]
[Manufacturer][Gigabyte Technology Co., Ltd.]
[Model][NULL]
[Name][Основная плата]
[OtherIdentifyingInfo][NULL]
[PartNumber][NULL]
[PoweredOn][True]
[Product][H55M-S2H]
[Removable][False]
[Replaceable][True]
[RequirementsDescription][NULL]
[RequiresDaughterBoard][False]
[SerialNumber][ ]
[SKU][NULL]
[SlotLayout][NULL]
[SpecialRequirements][NULL]
[Status][OK]
[Tag][Base Board]
[Version][x.x]
[Weight][NULL]
[Width][NULL]
Maybe c# is bad for retrieving such things?
I hope for solution on C/C++ or working solution on C#
Some motherboards simply don't have ID. It set to empty string.
So, if someone need to use motherboard unique thing for licensing purposes they should receive motherboard UUID.
Personally, I'd recommend using this particular Open Source hardware monitor library (you'll need the source). You can use it for hardware identification. Open Hardware Monitor
There is also a NuGet package called DeviceID. However, you will need to include their DLL with your package, but is a great fast, simple solution.
Here a usage example:
/* Depends on https://www.nuget.org/packages/DeviceId/ Install-Package DeviceId - Version 5.2.0*/
/* Using AddMacAddress(true, true) to exclude both virtual and wireless network adapters. */
readonly string MachineSupportID = new DeviceIdBuilder()
.AddMacAddress(true, true)
.AddMotherboardSerialNumber()
.AddProcessorId()
.AddSystemDriveSerialNumber()
.ToString();
May the force be with you.
I see references and hints that programmatically one can add a networked printer to a local computer using the ManagementClass and such. However I have not been able to find any actual tutorials on doing just this.
has anyone actually used the ManagementClass to do this?
I am doing this:
var connectionOption = new ConnectionOption();
var mgmScope = new ManagementScope("root\cimv2",connectionOptions);
var printerClass = new ManagementClass(mgmScope, new ManagementPath("Win32_Printer"),null);
var printerObj = printerClass.CreateInstance();
printerObj["DeviceID"] = prnName; //
printerObj["DriverName"] = drvName; // full path to driver
printerObj["PortName"] = "myTestPort:";
var options = new PutOptions {Type = PutType.UpdateOrCreate};
printerObj.Put(options);
All this does is create an error "Generic Failure"
I cant figure out what I am missing..... any help or thoughts about this would be appreciated.
I think I need to better explain what I am trying to do... when the printers needed are not tied to a print server, I need to:
create a tcpip raw port,
connect a printer via tcp/ip,
install drivers,
optionally set default.
I was hoping WMI could basically take care of all of this but it doesnt appear to be the case.
thanks!
The WMI Win32_Printer class provides a method called AddPrinterConnection to
add a network printer to the list of local printers. The code below
shows how to connect a network printer using the Win32_Printer class.
Please note, that under certain conditions the AddPrinterConnection fails
to connect the remote printer. In the example below I've listed the most
common error cases.
using (ManagementClass win32Printer = new ManagementClass("Win32_Printer"))
{
using (ManagementBaseObject inputParam =
win32Printer.GetMethodParameters("AddPrinterConnection"))
{
// Replace <server_name> and <printer_name> with the actual server and
// printer names.
inputParam.SetPropertyValue("Name", "\\\\<server_name>\\<printer_name>");
using (ManagementBaseObject result =
(ManagementBaseObject)win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null))
{
uint errorCode = (uint)result.Properties["returnValue"].Value;
switch (errorCode)
{
case 0:
Console.Out.WriteLine("Successfully connected printer.");
break;
case 5:
Console.Out.WriteLine("Access Denied.");
break;
case 123:
Console.Out.WriteLine("The filename, directory name, or volume label syntax is incorrect.");
break;
case 1801:
Console.Out.WriteLine("Invalid Printer Name.");
break;
case 1930:
Console.Out.WriteLine("Incompatible Printer Driver.");
break;
case 3019:
Console.Out.WriteLine("The specified printer driver was not found on the system and needs to be downloaded.");
break;
}
}
}
}
I spent almost the week on that issue! My target is a bit more complicated, I want to wrap the C# code in a WCF service called by a SharePoint Add-In, and this WCF service should call remotely the client to install the printers. Thus I tried hard with WMI.
In the mean time, I managed to use rundll32 solution (which requires first to create the port), and also did a powershell variant, really the simplest:
$printerPort = "IP_"+ $printerIP
$printerName = "Xerox WorkCentre 6605DN PCL6"
Add-PrinterPort -Name $printerPort -PrinterHostAddress $printerIP
Add-PrinterDriver -Name $printerName
Add-Printer -Name $printerName -DriverName $printerName -PortName $printerPort
The really tricky part is to know the name of the printer, it should match the string in the INF file!
But back to C# solution: I created a class with printerName, printerIP and managementScope as attributes. And driverName = printerName
private string PrinterPortName
{
get { return "IP_" + printerIP; }
}
private void CreateManagementScope(string computerName)
{
var wmiConnectionOptions = new ConnectionOptions();
wmiConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;
wmiConnectionOptions.Authentication = AuthenticationLevel.Default;
wmiConnectionOptions.EnablePrivileges = true; // required to load/install the driver.
// Supposed equivalent to VBScript objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
string path = "\\\\" + computerName + "\\root\\cimv2";
managementScope = new ManagementScope(path, wmiConnectionOptions);
managementScope.Connect();
}
private bool CheckPrinterPort()
{
//Query system for Operating System information
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TCPIPPrinterPort");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(managementScope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
if (m["Name"].ToString() == PrinterPortName)
return true;
}
return false;
}
private bool CreatePrinterPort()
{
if (CheckPrinterPort())
return true;
var printerPortClass = new ManagementClass(managementScope, new ManagementPath("Win32_TCPIPPrinterPort"), new ObjectGetOptions());
printerPortClass.Get();
var newPrinterPort = printerPortClass.CreateInstance();
newPrinterPort.SetPropertyValue("Name", PrinterPortName);
newPrinterPort.SetPropertyValue("Protocol", 1);
newPrinterPort.SetPropertyValue("HostAddress", printerIP);
newPrinterPort.SetPropertyValue("PortNumber", 9100); // default=9100
newPrinterPort.SetPropertyValue("SNMPEnabled", false); // true?
newPrinterPort.Put();
return true;
}
private bool CreatePrinterDriver(string printerDriverFolderPath)
{
var endResult = false;
// Inspired from https://msdn.microsoft.com/en-us/library/aa384771%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
// and http://microsoft.public.win32.programmer.wmi.narkive.com/y5GB15iF/adding-printer-driver-using-system-management
string printerDriverInfPath = IOUtils.FindInfFile(printerDriverFolderPath);
var printerDriverClass = new ManagementClass(managementScope, new ManagementPath("Win32_PrinterDriver"), new ObjectGetOptions());
var printerDriver = printerDriverClass.CreateInstance();
printerDriver.SetPropertyValue("Name", driverName);
printerDriver.SetPropertyValue("FilePath", printerDriverFolderPath);
printerDriver.SetPropertyValue("InfName", printerDriverInfPath);
// Obtain in-parameters for the method
using (ManagementBaseObject inParams = printerDriverClass.GetMethodParameters("AddPrinterDriver"))
{
inParams["DriverInfo"] = printerDriver;
// Execute the method and obtain the return values.
using (ManagementBaseObject result = printerDriverClass.InvokeMethod("AddPrinterDriver", inParams, null))
{
// result["ReturnValue"]
uint errorCode = (uint)result.Properties["ReturnValue"].Value; // or directly result["ReturnValue"]
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms681386(v=vs.85).aspx
switch (errorCode)
{
case 0:
//Trace.TraceInformation("Successfully connected printer.");
endResult = true;
break;
case 5:
Trace.TraceError("Access Denied.");
break;
case 123:
Trace.TraceError("The filename, directory name, or volume label syntax is incorrect.");
break;
case 1801:
Trace.TraceError("Invalid Printer Name.");
break;
case 1930:
Trace.TraceError("Incompatible Printer Driver.");
break;
case 3019:
Trace.TraceError("The specified printer driver was not found on the system and needs to be downloaded.");
break;
}
}
}
return endResult;
}
private bool CreatePrinter()
{
var printerClass = new ManagementClass(managementScope, new ManagementPath("Win32_Printer"), new ObjectGetOptions());
printerClass.Get();
var printer = printerClass.CreateInstance();
printer.SetPropertyValue("DriverName", driverName);
printer.SetPropertyValue("PortName", PrinterPortName);
printer.SetPropertyValue("Name", printerName);
printer.SetPropertyValue("DeviceID", printerName);
printer.SetPropertyValue("Location", "Front Office");
printer.SetPropertyValue("Network", true);
printer.SetPropertyValue("Shared", false);
printer.Put();
return true;
}
private void InstallPrinterWMI(string printerDriverPath)
{
bool printePortCreated = false, printeDriverCreated = false, printeCreated = false;
try
{
printePortCreated = CreatePrinterPort();
printeDriverCreated = CreatePrinterDriver(printerDriverPath);
printeCreated = CreatePrinter();
}
catch (ManagementException err)
{
if (printePortCreated)
{
// RemovePort
}
Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
}
}
finally install the driver. I still need further tests if its works on a clean Windows. Did many install/uninstall of the driver during my tests
In order to do this I ended up having to do a 2 stepper...
first build up a command line to fire off:
rundll32.exe printui.dll,PrintUIEntry /if /b "test" /f x2DSPYP.inf /r 10.5.43.32 /m "845 PS"
Then spawn it:
public static string ShellProcessCommandLine(string cmdLineArgs,string path)
{
var sb = new StringBuilder();
var pSpawn = new Process
{
StartInfo =
{
WorkingDirectory = path,
FileName = "cmd.exe",
CreateNoWindow = true,
Arguments = cmdLineArgs,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false
}
};
pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
pSpawn.Start();
pSpawn.BeginOutputReadLine();
pSpawn.WaitForExit();
return sb.ToString();
}
This seems to work... not the ideal method but for those printers not on a print server it seems to do the job.
I wanted to add answer as there is another way and Microsoft recommends using Microsoft.Management.Infrastructure now over System.Management.
Here is link to Microsoft documentation:
https://msdn.microsoft.com/en-us/library/microsoft.management.infrastructure.aspx
Please note that you have to add reference in c# project to the Microsoft.Management.Infrastructure dll:
Where the Microsoft.Management.Infrastructure
You may also need to configure winrm as well on machine
https://learn.microsoft.com/en-us/windows/desktop/winrm/installation-and-configuration-for-windows-remote-management
Here is sample code for adding a printer to local machine from a print server using c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Generic;
using Microsoft.Management.Infrastructure.Options;
namespace MMITest
{
class Program
{
static void Main(string[] args)
{
using (var session = CimSession.Create("localhost"))
{
using (var cimMethodParameters = new CimMethodParametersCollection())
{
cimMethodParameters.Add(CimMethodParameter.Create("Name", #"\\PrintServerName\PrinterName", CimFlags.Parameter));
session.InvokeMethod(#"root\cimv2", "Win32_Printer", "AddPrinterConnection", cimMethodParameters);
}
}
Console.ReadLine();
}
}
}
I've been trying to find a way to figure out which installed printers are 'connected'. After some Googling I figured I had to dive into WMI.
So I've built this test:
// Struct to store printer data in.
public struct MyPrinter
{
public string Availability;
public string ExtendedPrinterStatus;
public string Name;
public string PrinterStatus;
public string Status;
public string StatusInfo;
public MyPrinter(string a, string eps, string n, string ps, string s, string si)
{
Availability = a;
ExtendedPrinterStatus = eps;
Name = n;
PrinterStatus = ps;
Status = s;
StatusInfo = si;
}
}
var installedPrinters = new string[numPrinters];
PrinterSettings.InstalledPrinters.CopyTo(installedPrinters, 0);
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
var data = new List<MyPrinter>();
foreach (var printer in searcher.Get())
{
if (installedPrinters.Contains(printer["Name"].ToString()))
{
var availability = (printer["Availability"] ?? "").ToString();
var extendedPrinterStatus = (printer["ExtendedPrinterStatus"] ?? "").ToString();
var name = (printer["Name"] ?? "").ToString();
var printerStatus = (printer["PrinterStatus"] ?? "").ToString();
var status = (printer["Status"] ?? "").ToString();
var statusInfo = (printer["StatusInfo"] ?? "").ToString();
data.Add(new MyPrinter(availability, extendedPrinterStatus, name, printerStatus, status, statusInfo));
}
}
I have 6 printers from which 2 are network printers. I've run this with all printers connected and all results looked like this:
Availability = "" // printer["Availability"] = null
ExtendedPrinterStatus = "2" // 2 = Unknown
Name = "{printer name here}"
PrinterStatus = "3" // 3 = Idle
Status = "Unknown"
StatusInfo = "" // Null
So the only difference between the printers is the name.
I ran the script again but this time I disconnected my laptop from the network. So 2 of the printers were not connected anymore in this case.
The strange thing (for me) is, the results were exactly the same.
The reason I ran this test is, to figure out which field I'd need to use for my case.
So at the end, I have not been able to figure out how to figure out if a printer is connected or not.
So what I'd like, is a way to figure out the installed + connected printers in C#. If there is a way to do it without the use of WMI classes, that's also fine by me, as long as it works.
Me and a colleague have tried lots of stuff to find a solution for this and we figured this worked:
private string[] GetAvailablePrinters()
{
var installedPrinters = new string[PrinterSettings.InstalledPrinters.Count];
PrinterSettings.InstalledPrinters.CopyTo(installedPrinters, 0);
var printers = new List<string>();
var printServers = new List<string>();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
foreach (var printer in searcher.Get())
{
var serverName = #"\\" + printer["SystemName"].ToString().TrimStart('\\');
if (!printServers.Contains(serverName))
printServers.Add(serverName);
}
foreach (var printServer in printServers)
{
var server = new PrintServer(printServer);
try
{
var queues = server.GetPrintQueues();
printers.AddRange(queues.Select(q => q.Name));
}
catch (Exception)
{
// Handle exception correctly
}
}
return printers.ToArray();
}
The trick is that when a printserver is not available, GetPrintQueues will throw some specific exception. By only adding the printers that don't throw such an exception, we get a list of all the connected printers. This doesn't check if a printer is turned on/off because that actually doesn't matter. If it is turned off, the document will just be placed in the print queue and it can be printed later on.
I hope this helps others who bump into this problem.
Sidenote:
The reason I decided not to catch that specific exception, is because I would have to reference a dll just for that exception.
Currently I'm using following methods to get hardware information (network adapter, processor, hdd)
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
ManagementObject dsk = new ManagementObject(#"win32_logicaldisk.deviceid=""c:""");
My app is desktop, client-server (app and db are installed on server).
This methods get information for client. Is there a way to get hardware information for some node on lan - I want to get hardware information for server?
This is a subroutine I use in order to query remote hosts (here I assume I already configured WMI on the remote computer):
public string getWMI(string[] parameters)
{
string ip = parameters[0];
string username = parameters[1];
string password = parameters[2];
string query = parameters[3];
string result = "";
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope;
options.Username = username;
options.Password = password;
try
{
scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", options);
scope.Connect();
if (scope.IsConnected)
{
ObjectQuery q = new ObjectQuery(query);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, q);
ManagementObjectCollection objCol = searcher.Get();
foreach (ManagementObject mgtObject in objCol)
{
result = result + mgtObject.GetText(TextFormat.CimDtd20);
}
}
else
{
}
}
catch (Exception e)
{
writeLogFile("WMI Error: " + e.Message);
writeLog("WMI Error: " + e.Message);
}
return result;
}
In that subroutine I use a direct query such as "select * from Win32_ComputerSystem" but you can use ManagementClass as well.
I want to get hardware information for server?
WMI can poin to another server as long as:
The eserver exposes WMI
The fireawall does not block it.
Your user account has the rights on the other server.
Simple like that.
I need to make a WMI call in the constructor of my service. But when I start/restart the system this call takes significant amount of time.
I am using the following code to get the path of the windows service....
Here I've used the EnumerationOptions to improve the query performance, now in order to use it I have to use the ManagementScope which is "root\civm2", every time I've to use "root'civm2" as Management scope,
Earlier I was using managementObjectCollection.Count to know whether it contains any items or not, now to improve the performance I am using managementObjectEnumerator.MoveNext, will it help, I've commented the count related code.
Is there any better way to improve the performance of the same code...
EnumerationOptions options = new EnumerationOptions();
// options.Rewindable = false; **// I HAVE TO COMMENT OUT THIS IN ORDER TO GET THE RESULTS....**
options.ReturnImmediately = true;
string query = string.Format("SELECT PathName FROM Win32_Service WHERE Name = '{0}'", "MyService");
ManagementScope ms12 = new ManagementScope(#"root\cimv2");
ms12.Connect();
using (var managementObjectSearcher = new ManagementObjectSearcher(query))
{
managementObjectSearcher.Scope = ms12;
managementObjectSearcher.Options = options;
var managementObjectCollection = managementObjectSearcher.Get();
//if (managementObjectCollection.Count > 0)
//{
var managementObjectEnumerator = managementObjectCollection.GetEnumerator();
if (managementObjectEnumerator.MoveNext())
{
var invalidChars = new Regex(string.Format(CultureInfo.InvariantCulture, "[{0}]", Regex.Escape(new string(Path.GetInvalidPathChars()))));
var path = invalidChars.Replace(managementObjectEnumerator.Current.GetPropertyValue("PathName").ToString(), string.Empty);
Console.WriteLine(path);
}
//}
else
{
Console.WriteLine("Else part...");
}
}
Am I using the scope and EnumerationOption in correct way??
Please guide.
As the answer to your another question suggest you can build the object path of the class and use the ManagementObject directly to improve the performance , now if you want to check if the ManagementObject return an instance you can use the private property IsBound.
string ServicePath = string.Format("Win32_Service.Name=\"{0}\"", "MyService");
var WMiObject = new ManagementObject(ServicePath);
PropertyInfo PInfo = typeof(ManagementObject).GetProperty("IsBound", BindingFlags.NonPublic | BindingFlags.Instance);
if ((bool)PInfo.GetValue(WMiObject, null))
{
string PathName = (string)WMiObject.GetPropertyValue("PathName");
var invalidChars = new Regex(string.Format(CultureInfo.InvariantCulture, "[{0}]", Regex.Escape(new string(Path.GetInvalidPathChars()))));
var path = invalidChars.Replace(PathName, string.Empty);
Console.WriteLine(path);
}
else
{
Console.WriteLine("Else part...");
}
It appears that in more recent versions of the .NET framework the binding happens as late as possible. At least this was the case for me when I was testing the existence of a specific shared folder.
Here is an update to #RRUZ 's solution which uses a try-catch instead of reflecting the IsBound internal property.
var servicePath = string.Format("Win32_Service.Name=\"{0}\"", "MyService");
string pathName = null;
try
{
var wmiObject = new ManagementObject(servicePath);
pathName = (string)wmiObject.GetPropertyValue("PathName");
}
catch {}
if (pathName != null)
{
var invalidChars = new Regex(string.Format(CultureInfo.InvariantCulture, "[{0}]", Regex.Escape(new string(Path.GetInvalidPathChars()))));
var path = invalidChars.Replace(pathName, string.Empty);
Console.WriteLine(path);
}
else
{
Console.WriteLine("Else part...");
}