c# - Windows 服务更改/删除注册表值

标签 c# windows service registry

我有控制台应用程序,它正在努力删除注册表值。但是当我尝试在服务中做同样的事情时,它不起作用。

txt 文件的日志工作正常,所以我每 5 秒查看一次日志。但是 DeleteValue 方法的异常说:

Value doesn't exist

控制台应用程序(工作):

class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                KillProxy();
                Console.ReadLine();
            }
        }

        private static void KillProxy()
        {
            string keyName = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
            {
                try
                {
                    key.DeleteValue("AutoConfigURL");
                    key.Close();
                }
                catch { }
            }
        }
    }

服务应用程序(不工作):

public partial class Service1 : ServiceBase
    {
        private Timer t1;

        /// <summary>
        /// Init
        /// </summary>
        public Service1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Service start event
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            t1 = new Timer();
            t1.Interval = 5000;
            t1.Elapsed += new ElapsedEventHandler(t1_tick);
            t1.Enabled = true;
        }

        /// <summary>
        /// Service stop event
        /// </summary>
        protected override void OnStop()
        {
            t1.Enabled = false;
        }

        /// <summary>
        /// Timer tick event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t1_tick(object sender, ElapsedEventArgs e)
        {
            KillProxy();
        }

        /// <summary>
        /// If key AutoConfigURL exists, delete it
        /// </summary>
        private void KillProxy()
        {
            string keyName = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
            {
                try
                {
                    WriteLog("start deleting ");
                    key.DeleteValue("AutoConfigURL");
                    key.Close();
                }
                catch (Exception ex)
                {
                    WriteLog(ex.Message.ToString());
                }
            }
        }


        private void WriteLog(string msg)
        {
            StreamWriter sw = null;

            try
            {
                sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\logfile.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + " " + msg);
                sw.Flush();
                sw.Close();
            }
            catch
            {
                //nothing
            }
        }
    }

最佳答案

原因是您正在访问 CurrentUser 注册表子树,正如其名称所示,它是特定于用户的,

当您在控制台模式下运行代码时,您会与您的用户一起运行它,并且检索到的 CurrentUser 注册表部分的内容是您的,其中包含您知道的键和子键,

当您将代码作为服务运行时,用于运行的默认帐户是本​​地服务或系统,在这种情况下,CurrentUser 注册表子树绝对不是您的,因为它是在控制台模式下加载的。

您应该将服务设置为使用您自己的凭据运行(您必须输入用户名和密码)或让应用程序在控制台模式下运行,或者重新访问您的应用程序逻辑,具体取决于您需要做什么。

关于c# - Windows 服务更改/删除注册表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48902094/

相关文章:

c# - 在 C# MVC 项目中将 session 添加到伪造的 httpContext

c# - 如何在控制台应用程序中配置 ConsoleLogger?

asp.net - 缓存 DTO 或缓存来自昂贵资源的数据

Python Windows 服务 pyinstaller 可执行文件错误 1053

c# - 为什么 SQL Server 数据库用户写入错误的架构?

c# - 将多头列表转换为字符串数组

windows - IFileDialog:添加自定义位置 - 并选择该位置

windows - TextPad 和 Unicode : full support?

Unix 'which' 实用程序的 Windows/Powershell 版本

对象属性的 C# Web 服务序列化