I have simple Windows Service (Topshelf) written in .NET. When I try to use ManagementObjectSearcher (specifically to search connected disk drives) I get error:
The specified service does not exist as an installed service. (Exception from HRESULT: 0x80070424)
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
searcher.Get())
Everything else works fine (eg. using repository to access DB), only fails on this.
Related
As noted, I'm on a Windows 8 machine using Visual Studio 2012.
I need to map/unmap network drives as well as get a list of all of the currently mapped drives.
To map/unmap I'm using WNetAddConnection2A and WNetCancelConnection2A, respectively, via PInvoke.
To get a list of the currently mapped drives, I'm currently using WMI and querying Win32_MappedLogicalDisk.
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_MappedLogicalDisk"))
{
foreach (ManagementObject queryObj in searcher.Get())
{
}
}
The interesting bit is that the returned "mapped" drives from WMI do not show in the left panel of File Explorer under "Computer." The returned "mapped" drives also do not show using the "net use" command. I can map a drive via WNetAddConnection2A and it will be returned through the Win32_MappedLogicalDisk query but it will not show in File Explorer.
Additionally, I can manually map a drive via File Explorer and the mapped drive will show using "net use" but will not be returned using WMI to query Win32_MappedLogicalDisk.
File Explorer / "net use" don't seem to be talking to the same sources as WNetAddConnection2A / WMI.
I've tested this same code on Windows 7 without any issues.
Any help would be much appreciated. Thanks
Try the Win32_LogicalDisk WMI class and the DriveType property, (The value 4 indicates a Network Drive).
Select * From Win32_LogicalDisk Where DriveType = 4
I am developing a C# windows service.
I need to know (from the service) who is the currently logged on user.
I tried to follow the answer in here with this code:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
But the problem is that it works only on Windows 7, but on Windows 8 I get an empty string.
Was the WMI changed in Windows 8?
The problem wasn't with windows 8 but the fact that I used remote connection.
This WMI doesn't work for remote connection.
please help me as i need to kill/ restart a process remotely.
I have tried this but no luck for me. Error was - "Couldn't connect to remote machine"
Process[] prs = Process.GetProcesses("SERVERNAME");
foreach (Process pr in prs)
{
if (pr.ProcessName == "process")
{
pr.Kill();
}
}
I also try this one but still doesn't work and got this error:
"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
ManagementScope scope = new ManagementScope("\\\\SERVERNAME\\root\\cimv2");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process WHERE Name='process'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objectCollection = searcher.Get();
foreach (ManagementObject managementObject in objectCollection)
{
managementObject.InvokeMethod("Terminate", null);
}
I hope someone has a solution for this.Thanks in advance!
Well, the errors are actually telling you exactly what's wrong. Your application is failed connecting to the remote machine. This can happen for some reasons:
The remote computer is unavailable (not exists)
The remote computer doesn't have WMI open for remote connection, or it's firewall-ed on the remote machine or on the way.
You don't have a permission to perform this action. Since i don't see anywhere in your code impersonation, then the connection will be using the current process token (which account it is? depends on your configuration on the application pool, or in your web.config if you use tag, by default - it's NT AUTHORITY). You probably don't have permissions to access the remote machine using WMI with the credentials you're running at. It can be solved by impersonation in your code, in the web.config, or in the application pool settings to an account which has the sufficient permission (if you're in domain, ask your IT department for an account).
Good luck.
Add this to your web.config with the appropriate account (the one with permissions).
<identity impersonate="true" userName="UserWithPermissions" password="pw" />
Is there any way (in C#, using WMI classes) to find out that how many times a particular software has been installed and uninstalled?
I want to run it on remote computer. I am getting software list by following code:
ManagementScope scope = new ManagementScope(#"\\" + ipAddress + #"\root\cimv2");
ObjectQuery query = new ObjectQuery("Select * from Win32_Product");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
Console.Write(m["Caption"]+"\t");
Console.WriteLine(m["installDate"]);
}
Normally not.
When a program will be uninstalled every bit of the program should be removed from the machine like it was never their. Unfortunately nearly every program doesn't make a perfect job at this point leaving some artifacts on the machine.
Nevertheless the desired behavior is that after a uninstall everything is gone (including some kind of counter) so that it is only possible to check if a program is currently installed or not.
On the other site nothing permits a program to save somewhere some counter (e.g. registry) which will increased everytime a installation is started, but that's something specific for each program and no common mechanism exists where this counter should reside.
Please any one help me to get all network printers.
I get all printers installed in the local machine using "System.Drawing.Printing.PrinterSettings.InstalledPrinters".
But I can't get the printers which are in the Network.
I try with "ManagementObjectSearcher" but I can't access this class.
I think it does not support in framework 4.0.
I'm using ASP.NET 4.0, C#. Any help will be greatly appreciated.
Thanks
Singaravelu.R.
if you cannot find/reference the ManagementObjectSearcher Class probably is because you did not add the proper reference to: System.Management.dll to your C# project. Surely it is supported also by .NET 4.
as you can see in this question: ManagementObjectSearcher select network printers? you can find all network printers in this way:
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Printer");
var results = searcher.Get();
IList<ManagementBaseObject> printers = new List<ManagementBaseObject>();
foreach (var printer in results) {
if ((bool)printer["Network"]) {
printers.Add(printer);
}
}