简体   繁体   English

如何在C#中阅读Windows Server 2008

[英]How to read Windows Server 2008 in c#

i am trying o read registry value with the following code. 我正在尝试使用以下代码读取注册表值。

    Label1.Text = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Myweb\\ConnectionManager", "ID", null).ToString();

it work fine when i am trying in windows xp but i never works in windows server 2008. any help Please 当我在Windows XP中尝试时,它工作正常,但我从未在Windows Server 2008中工作。任何帮助,请

You are probably running into the issue of the WOW redirector . 您可能正在遇到WOW重定向器的问题。

You can use the more specialized classes in Microsoft.Win32 for getting either a 64 or 32 bits part of the hive. 您可以在Microsoft.Win32中使用更专业的类来获取配置单元的64位或32位。

 var root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine
                                   ,RegistryView.Registry32); // or Registry64
 var flk = root.OpenSubKey("SOFTWARE");
 var slk = flk.OpenSubKey("Myweb");
 var tlk = slk.OpenSubKey("ConnectionManager");
 var val = tlk.GetValue("ID");

 Label1.Text = val.ToString();

Or a more general purpose method to get a registry value whatever it takes: 或更通用的方法来获取注册表值,不管它是什么:

object GetValue64Or32(string path, string ValueKey)
{
     var parts = path.Split('\\');
     RegistryHive hive = RegistryHive.LocalMachine;
     switch(parts[0])
     {
        case "HKEY_LOCAL_MACHINE":
            hive = RegistryHive.LocalMachine;
        break;
        default:
           throw new NotImplementedException();
     }
     foreach(var view in Enum.GetValues(typeof(RegistryView)))
     {
        var key = RegistryKey.OpenBaseKey(hive, (RegistryView) view);
        for(var partIndex=1; partIndex<parts.Length;partIndex++)
        {
           key = key.OpenSubKey(parts[partIndex]);
           if (key == null) break;
        }
        if (key!=null) return key.GetValue(ValueKey);
     }
     return null;
}

Usage: 用法:

var value = GetValue64Or32(
               "HKEY_LOCAL_MACHINE\\SOFTWARE\\Myweb\\ConnectionManager"
             , "ID");
Label1.Text = value!=null?value.ToString():"no value found";

If I use this registry file this code works for the 32 bits hive: 如果我使用此注册表文件,则此代码适用于32位配置单元:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MyWeb\]

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MyWeb\ConnectionManager\]
"ID"="id 1"

And this works for the 64 bits hive: 这适用于64位配置单元:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\MyWeb\]

[HKEY_LOCAL_MACHINE\SOFTWARE\MyWeb\ConnectionManager\]
"ID"="id 1"

You can use REG from the commandprompt to verify if your registrypath exists: 您可以从命令提示符中使用REG来验证您的注册表路径是否存在:

reg query HKLM\Software\MyWeb\ConnectionManager /s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Windows Server 2008的Windows服务中的读取设置属性(C#) - Read Settings properties (C#) in a Windows service with Windows server 2008 如何使用C#在Windows Server 2008中写入事件日志? - How to write to eventlog in Windows Server 2008 using C#? 如何在C#中检测Windows Server 2008上的防病毒软件? - How to detect antivirus on Windows Server 2008 in C#? 如何在 c# 中列出 Windows Server 2008 的已安装功能 - How to list installed features of Windows Server 2008 in c# Windows 7/2008 Server上的C#事件记录 - Event Logging in C# on Windows 7/ 2008 Server C# 确定操作系统是 Windows 7 还是 Windows Windows Server 2008 - C# determine OS is Windows 7 or WIndows Windows Server 2008 如何使用C#,Entity Framework和SQL Server 2008读/写地理数据? - How to read / write geography data using C#, Entity Framework and SQL Server 2008? 在Windows Server 2008 64 C#中解析CSV文件 - Parsing CSV file in Windows Server 2008 64 C# 通过C#访问Windows 7和Server 2008的远程注册表 - Accessing Remote Registry of Windows 7 and Server 2008 through C# 从Windows Server 2008上的C#应用​​程序读取文档标签 - Reading document tags from C# application on Windows server 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM