Hi,
Is it possible to read registry of a remote machine using WMI in C#. I have written the following program to read the registry of a remote machine.But It gives exception as Access is denied.Please suggest me what needs to be changed.
using System;
using System.Management;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
namespace SystemInfoCollectorinCS
{
/// <summary>
/// CSC.cc.assay: This class collects system information of local machine.
/// </summary>
class SystemInfoCollectorinCS
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try{
ManagementScope managementScope;
string regKeyToGet=null, keyToRead=null;
string serverName="ip address of remote machine";
string adminUser="userid";
string adminUserPassword="paswword";
ConnectionOptions connectionOptions = new ConnectionOptions();
string wmiPath;
if(serverName == null || serverName.Length == 0 || adminUser == null || adminUser.Length == 0 || adminUserPassword == null ||
adminUserPassword.Length == 0)
{
//specify WMI path for local machine
wmiPath = @"root\cimv2";
}
else
{
//set connection parameters for remote machine
connectionOptions.Username = adminUser;
connectionOptions.Password = adminUserPassword;
//specify WMI path for remote machine
Console.WriteLine("Setting the WMI path");
wmiPath = String.Format(@"\\{0}\root\cimv2", serverName);
Console.WriteLine("Setting the WMI path is done");
}
Console.WriteLine("Setting the connection");
managementScope = new ManagementScope(wmiPath, connectionOptions);
Console.WriteLine("Setting the connection is done");
Console.WriteLine("Accessing the registry of remote");
ManagementClass registry = new ManagementClass(managementScope, new ManagementPath("StdRegProv"), null);
Console.WriteLine("Accessing the registry of remote machine is done");
// Returns a specific value for a specified key
Console.WriteLine("Returning a specific value for a specified key");
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue");
Console.WriteLine("Returning a specific value for a specified key is done");
inParams["sSubKeyName"] = regKeyToGet;
inParams["sValueName"] = keyToRead;
Console.WriteLine("Invokeing Method");
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null);
Console.WriteLine("Invokeing Method");
Console.WriteLine(outParams["sValue"].ToString());
}// try
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
//err = ex.Message.ToString();
}
}
}
}
Bookmarks