注册表没有用C#应用程序修改 [英] Registry not being modified with C# application

查看:152
本文介绍了注册表没有用C#应用程序修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hey guys,我有一个在Windows服务中运行的代码。



服务监视LAN并相应地更改代理设置,问题是因为某些原因代理设置未更新。



应用程序正在本地系统帐户下运行,但注册表std ldoes nto更改:

$

 使用(var key = Registry.CurrentUser.OpenSubKey @Software\Microsoft\Windows\CurrentVersion\Internet Settings,true))
{
if(key!= null)
{
key.SetValue ProxyEnable,Convert.ToInt32(enable),RegistryValueKind.DWord);

// I / O Flushes
key.Flush();
key.Close();
return;
}

//此处抛出的异常,用于测试键是否有效。
}


解决方案

本地系统帐户,没有HKEY_Current_User。



事实上,在许多情况下,即使您以用户帐户身份运行服务,也不会有HKEY_CURRENT_USER,因为服务通常不会创建完整的Windows站 - 它只是使用用户帐户进行身份验证,而不是配置文件。



这是许多COM自动化应用程序无法使用的几个原因之一



您将可以访问HKEY_LOCAL_MACHINE。



/ strong>
如果您以特定用户帐户运行服务(或者您的服务中的线程模拟特定的用户帐户),您可以使用RegOpenCurrentUser函数检索键的句柄想要,然后使用RegistryKey.FromHandle从.Net API的使用它。


hey guys, i have the following snipped of code that is running within a Windows Service.

the service watches the LAN and changes the proxy settings accordingly, the problem is that for some reason the proxy settings are not being updated.

the application is running under the Local System Account but the registry stil ldoes nto change:

anyone have any ideas?

using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true))
{
    if (key != null)
    {
        key.SetValue("ProxyEnable", Convert.ToInt32(enable), RegistryValueKind.DWord);

        //I/O Flushes
        key.Flush();
        key.Close();
        return;
    }

    //Exception thrown here to test if key is valid.
}

解决方案

If you are running in the local system account, there is no HKEY_Current_User.

In fact, in many cases, even if you are running a service as a user account, there will be no HKEY_CURRENT_USER, because the service generally won't create a full windows station -- it just uses the user account for authentication, and not the profile.

This is 1 of several reasons that many COM automation-capable applications are not able to be used by services.

You will be able to access HKEY_LOCAL_MACHINE, though.

Edit If you run your service as a specific user account (or if you have the thread in your service impersonate a specific user account), you might be able to use RegOpenCurrentUser function to retrieve a handle to the key you want, and then use RegistryKey.FromHandle to use it from the .Net API's.

这篇关于注册表没有用C#应用程序修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆