c#操作远程注册表的两种方法

来源:互联网 发布:linux怎样安装压缩文件 编辑:程序博客网 时间:2024/06/15 16:21
使用remote register service服务来操作注册表: 

代码:

RegistryKey environment_key; string      remote_name = host; string      sub_key = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; try     {         environment_key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, remote_name).OpenSubKey(sub_key);         foreach (string soft in environment_key.GetSubKeyNames())         {             if (soft.StartsWith("{") || soft.EndsWith("}"))             {                 continue;             }             else             {                 Regex r = new Regex("^KB[0-9]");                 Match m = r.Match(soft);                 if (m.Success)                 {                     continue;                 }             }             Console.WriteLine(soft);             }         }         catch         {             Console.WriteLine("Query soft list host error.", host);             return;         } 

使用WMI来操作注册表: 
代码:

ManagementClass    reg = null; string    local_host = Dns.GetHostName( ); string    sub_key = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; if (host == local_host) {     reg = new ManagementClass("\\localhost\root\DEFAULT:StdRegProv"); } else {     reg = new ManagementClass("\\" + host + "\root\DEFAULT:StdRegProv"); } try {     object[] method_args = { 0x80000002, sub_key, null };     object result = reg.InvokeMethod("EnumKey", method_args);     string[] softs = (string[])method_args[2];     foreach (string soft in softs)     {         if (soft.StartsWith("{") || soft.EndsWith("}"))         {             continue;         }         else         {             Regex r = new Regex("^KB[0-9]");             Match m = r.Match(soft);             if (m.Success)             {                 continue;             }         }         Console.WriteLine(soft);     } } catch {     Console.WriteLine("Query soft list host error.", host);     return; } 


0 0