using WMI in CSharp

L

Lalit

Hi,

how can we get registry settings of a computer within local network using
WMI?

Lalit
 
N

Nicholas Paldino [.NET/C# MVP]

Lalit,

I don't know that you can get actual registry values through WMI. The
only class I see for this is the Win32_Registry class, which gives
information about the registry as a whole.

If you want to get actual values, you can call the static
OpenRemoteBaseKey method on the RegistryKey class in the Microsoft.Win32
namespace to get the RegistryKey on a remote machine.
 
L

Lalit

Thanks for the reply, I got a code from code project to read registry values
using WMI.

I stunk on one more problem now. I was able to get version number of com
dll but I want to get version of .Net dll and for .Net dlls it return NULL
in Version property.

Any Idea?
Lalit


Nicholas Paldino said:
Lalit,

I don't know that you can get actual registry values through WMI. The
only class I see for this is the Win32_Registry class, which gives
information about the registry as a whole.

If you want to get actual values, you can call the static
OpenRemoteBaseKey method on the RegistryKey class in the Microsoft.Win32
namespace to get the RegistryKey on a remote machine.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lalit said:
Hi,

how can we get registry settings of a computer within local network using
WMI?

Lalit
 
W

Willy Denoyette [MVP]

Lalit said:
Thanks for the reply, I got a code from code project to read registry
values
using WMI.

I stunk on one more problem now. I was able to get version number of com
dll but I want to get version of .Net dll and for .Net dlls it return NULL
in Version property.

Any Idea?
Lalit

You need to use the StdRegProv class in WMI's root\default namespace.
Following snippet reads a DWORD value from HKCU to get you started. Consult
the WMI docs in MSDN for more details....

....
const uint HKEY_CURRENT_USER = 0x80000001;
ConnectionOptions co = new ConnectionOptions();
co.Username = "administrator"; // user with sufficient privs to read the
reg key
co.Password = "xxxxxxx"; // his pwd
string remMachine = "ssssss"; // remote machine name
ManagementPath p = new ManagementPath("StdRegProv");
ManagementScope scope = new ManagementScope(@"\\" + remMachine +
@"\root\default", co);
using (ManagementClass regClass = new ManagementClass(scope, p, null))
{
ManagementBaseObject inputArgs =
regClass.GetMethodParameters("GetDWORDValue");
inputArgs["hDefKey"] = HKEY_CURRENT_USER;
inputArgs["sSubKeyName"] = "Console";
inputArgs["sValueName"] = "HistoryBufferSize";
ManagementBaseObject outParams = regClass.InvokeMethod("GetDWORDValue",
inputArgs, null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret == 0)
Console.WriteLine("Success: {0}",
(uint)(outParams.Properties["uValue"].Value));
else Console.WriteLine("Failed with error code: {0}", ret);
}
....

Willy.
 
W

Willy Denoyette [MVP]


And here is another sample that ilustrates how you can read an expanded
value from a remote systems registry. The sample reads the expanded %windir%
value from the system's environment, this value expands to the path where
the current running OS is stored.



static void GetExpandedWindir()
{
ConnectionOptions co = new ConnectionOptions();;
co.Username = "administrator";
co.Password = "xxxxx";
string remMachine = "ssssss";
ManagementPath p = new ManagementPath(@"StdRegProv");
ManagementScope scope = new ManagementScope(@"\\" + remMachine +
@"\root\default", co);
using (ManagementClass regClass = new ManagementClass(scope, p, null))
{
ManagementBaseObject inputArgs =
regClass.GetMethodParameters("GetExpandedStringValue");
inputArgs["sSubKeyName"] = @"SYSTEM\CurrentControlSet\Control\Session
Manager\Environment";
inputArgs["sValueName"] = "windir";
ManagementBaseObject outParams =
regClass.InvokeMethod("GetExpandedStringValue", inputArgs, null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret == 0)
Console.WriteLine("Success: {0}",
(string)(outParams.Properties["sValue"].Value));
else Console.WriteLine("Failed with error code: {0}", ret);
}
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top