Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#Can';无法从Windows服务中达到注册表值_C#_Windows Services_Registry - Fatal编程技术网

c#Can';无法从Windows服务中达到注册表值

c#Can';无法从Windows服务中达到注册表值,c#,windows-services,registry,C#,Windows Services,Registry,我在从服务应用程序的注册表读取值时遇到问题。我是.NET服务应用程序部分的新手,但我在表单应用程序中使用了此代码。一定有什么不同,因为它在服务应用程序中不起作用。总之,这是我平时阅读的代码: public string GetValue(string Name, string Path = "") { try { RegKey = Registry.CurrentUser.OpenSubKey(DefaultFolder + @P

我在从服务应用程序的注册表读取值时遇到问题。我是.NET服务应用程序部分的新手,但我在表单应用程序中使用了此代码。一定有什么不同,因为它在服务应用程序中不起作用。总之,这是我平时阅读的代码:

 public string GetValue(string Name, string Path = "")
    {
        try
        {
            RegKey = Registry.CurrentUser.OpenSubKey(DefaultFolder + @Path + @"\");
            if (RegKey != null)
            {
                object TempObj = RegKey.GetValue(Name);
                if (TempObj == null) return "-13003";
                Type T = TempObj.GetType();
                if (T.IsArray)
                {
                    string[] TempArray = (string[])TempObj;
                    string output = "";
                    for (int i = 0; i < TempArray.Length; i++)
                    {
                        output += TempArray[i].Substring(0, TempArray[i].Length - 1);
                        if (i != TempArray.Length) output += "\n";
                    }

                    return output;
                }
                else
                {
                    return RegKey.GetValue(Name).ToString();
                }
            }
            else
            {
                return "-13001";
            }
        }
        catch (Exception ex)
        {
            return "-13002";
        }
    }
它不起作用了

到目前为止,我知道RegKey变量在

RegKey = Registry.CurrentUser.OpenSubKey(DefaultFolder + @Path + @"\");
这句话我不知道为什么

我在谷歌上搜索了一下,找到了这样的东西

RegistryKey rb = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
RegKey = rb.OpenSubKey(DefaultFolder + @"\" + Name, false);
但是结果是一样的,我的RegKey变量仍然是null

我只想在如下路径下读取一个值:
计算机\HKEY\U CURRENT\U USER\Software\ServiceReadLocation\Values

Registry.CurrentUser
RegistryHive.CurrentUser
在Windows服务
LocalSystem
中执行时,将无法按预期工作,很可能是您遇到的
null
的原因

MSDN:

注册表项HKEY\U CURRENT\U USER与默认用户关联,与当前用户关联。要访问其他用户的配置文件,请模拟该用户,然后访问HKEY_CURRENT_user

所以我可以看到太多的选择

  • 可以说,最简单的方法是以该用户的身份运行服务
  • 以LocalSystem身份运行服务,模拟用户,然后在需要时访问注册表。阅读后,您可以停止模拟

  • 服务运行在哪个帐户中?如果您在InitializeComponent方法内以LocalSystemIn-serviceProcessInstaller的方式运行,则CurrentUser不适用。我已将帐户设置为System.ServiceProcess.ServiceAccount.LocalSystem是否应将其更改为System.ServiceProcess.ServiceAccount.User?但是,我必须知道用户名和密码,对吗?这取决于您试图实现的目标。我尝试使用注册表中的Computer\HKEY\U CURRENT\U user\Software\ServiceReadLocation\values路径下获得的值进行一些操作。我可以打开此路径并从表单应用程序中读取,但我无法在服务应用程序中执行此操作。我不确定您的解决方案是否有效,我将告诉您我是如何做到的。我将设置更改为本地计算机而不是当前用户,因为我不希望每个用户都更改设置。之后,我使用了RegistryKey rb=RegistryKey.OpenBaseKey(RegistryHive.CurrentUser,RegistryView.Default);RegKey=rb.OpenSubKey(DefaultFolder+@“\”+名称,false);OpenBaseKey而不是OpenSubKey,一切都正常因为我“正在”使用当前用户注册表项,然后我意识到(在与老板进行了一次简短的演讲后)我不需要将我的设置放入CurrentUser,而是开始使用本地机,抱歉,如果我侮辱了你,我只是说我不知道你的解决方案是否有效,因为我没有尝试:)但是如果你这么说,那么它会有效。感谢链接,我很高兴我知道CurrentUser只是一个快捷方式,我将从现在起使用basekey。
    RegistryKey rb = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
    RegKey = rb.OpenSubKey(DefaultFolder + @"\" + Name, false);