Process、管理者权限、注册表、xml修改

       //判断是否有管理者权限
            WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                return;
            }
            //检索指定路径的注册表,true为写访问权限。
            RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey(@"Software\软件名\10.0\Test",true);
            //获取指定注册表键值
            object obj = softwareKey.GetValue("CliUpdate");
            if (softwareKey.GetValue("CliUpdate") != null && obj.ToString() == "ZK")
            {
                return;
            }
            string processName = "进程名称";
            //根据名称获取进程
            var result = Process.GetProcesses().FirstOrDefault(o => { return o.ProcessName.Contains(processName); });
            if (result != null)
            {
                //Process.MainModule 需要管理者权限 才能获取
                string path = Path.GetDirectoryName(result.MainModule.FileName);//通过路径返回目录
                string goalFile = Path.Combine(path, "XXXXX.config");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(goalFile);
                XmlNode xnode = xmlDoc.SelectSingleNode("Config/RemoteUrl");//获取相关节点
                if (xnode != null)
                {
                    //修改节点值
                    xnode.InnerText = "www.baidu.com";
                    xmlDoc.Save(goalFile);
                    //添加注册表新的键
                    softwareKey.CreateSubKey("CliUpdate");
                    softwareKey.SetValue("CliUpdate", "ZK");
                }
            }

猜你喜欢

转载自www.cnblogs.com/zacklau/p/11313418.html