Подключение к удаленному компьютеру через WMI - C#
Формулировка задачи:
вот код, который использую:
выдает ошибку "отказано в доступе". компы не в домене, если это важно.
Што не так?
Листинг программы
- ConnectionOptions options = new ConnectionOptions();
- options.Username = "u";
- options.Password = "123";
- scope = new ManagementScope("\\\\PHILKA\\root\\CIMV2", options);
- scope.Connect();
Решение задачи: «Подключение к удаленному компьютеру через WMI»
textual
Листинг программы
- namespace ReadRegistry
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- private List<string> ReadRemoteRegistryusingWMI(string machineName)
- {
- List<string> programs = new List<string>();
- ConnectionOptions connectionOptions = new ConnectionOptions();
- connectionOptions.Username = textBox1.Text;
- connectionOptions.Password = textBox2.Text;
- //ManagementScope scope = new ManagementScope("\\\\" + machineName + "\\root\\CIMV2", connectionOptions);
- ManagementScope scope = new ManagementScope("\\\\" + machineName + "\\root\\DEFAULT", connectionOptions);
- scope.Connect();
- string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
- ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);
- ManagementBaseObject inParams = registry.GetMethodParameters("EnumKey");
- inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
- inParams["sSubKeyName"] = softwareRegLoc;
- // Read Registry Key Names
- ManagementBaseObject outParams = registry.InvokeMethod("EnumKey", inParams, null);
- string[] programGuids = outParams["sNames"] as string[];
- foreach (string subKeyName in programGuids)
- {
- inParams = registry.GetMethodParameters("GetStringValue");
- inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
- inParams["sSubKeyName"] = softwareRegLoc + @"\" + subKeyName;
- inParams["sValueName"] = "DisplayName";
- // Read Registry Value
- outParams = registry.InvokeMethod("GetStringValue", inParams, null);
- if (outParams.Properties["sValue"].Value != null)
- {
- string softwareName = outParams.Properties["sValue"].Value.ToString();
- programs.Add(softwareName);
- }
- }
- return programs;
- }
- private List<string> ReadMonitorusingWMI(string machineName)
- {
- string sSerial = "";
- string sModel = "";
- //Open the Display Reg-Key
- List<string> programs = new List<string>();
- ConnectionOptions connectionOptions = new ConnectionOptions();
- connectionOptions.Username = textBox1.Text;
- connectionOptions.Password = textBox2.Text;
- ManagementScope scope = new ManagementScope("\\\\" + machineName + "\\root\\DEFAULT", connectionOptions);
- scope.Connect();
- string softwareRegLoc = @"SYSTEM\CurrentControlSet\Enum\DISPLAY";
- ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);
- ManagementBaseObject inParams = registry.GetMethodParameters("EnumKey");
- inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
- inParams["sSubKeyName"] = softwareRegLoc;
- // Read Registry Key Names
- ManagementBaseObject outParams = registry.InvokeMethod("EnumKey", inParams, null);
- string[] programGuids = outParams["sNames"] as string[];
- foreach (string subKeyName in programGuids)
- {
- inParams = registry.GetMethodParameters("EnumKey");
- inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
- inParams["sSubKeyName"] = softwareRegLoc + @"\" + subKeyName;
- // Read Registry Key Names
- outParams = registry.InvokeMethod("EnumKey", inParams, null);
- string[] programGuids2 = outParams["sNames"] as string[];
- foreach (string subKeyNameNext in programGuids2)
- {
- inParams = registry.GetMethodParameters("EnumKey");
- inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
- inParams["sSubKeyName"] = softwareRegLoc + @"\" + subKeyName + @"\" + subKeyNameNext;
- // Read Registry Key Names
- outParams = registry.InvokeMethod("EnumKey", inParams, null);
- string[] programGuids3 = outParams["sNames"] as string[];
- //Check if Monitor is active
- if (programGuids3.Contains("Control"))
- {
- if (programGuids3.Contains("Device Parameters"))
- {
- inParams = registry.GetMethodParameters("GetBinaryValue");
- inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
- inParams["sSubKeyName"] = softwareRegLoc + @"\" + subKeyName + @"\" + subKeyNameNext + @"\" + "Device Parameters";
- inParams["sValueName"] = "EDID";
- // Read Registry Binary Value
- outParams = registry.InvokeMethod("GetBinaryValue", inParams, null);
- byte[] bObj = outParams["uValue"] as byte[];
- if (bObj != null)
- {
- sSerial = "";
- sModel = "";
- //Get the 4 Vesa descriptor blocks
- string[] sDescriptor = new string[4];
- sDescriptor[0] = Encoding.Default.GetString(bObj, 0x48, 18);
- sDescriptor[1] = Encoding.Default.GetString(bObj, 0x6C, 18);
- sSerial = sDescriptor[0].Substring(5).Replace(@"\0\0\0y\0", "").Trim();
- sModel = sDescriptor[1].Substring(5).Replace(@"\0\0\0y\0", "").Trim();
- rtbox.Text = sSerial;
- rtbox2.Text = sModel;
- }
- }
- }
- }
- }
- return programs;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д