当前位置: 移动技术网 > IT编程>开发语言>.net > C#修改注册表

C#修改注册表

2018年01月04日  | 移动技术网IT编程  | 我要评论

棉鞋里的阳光说课稿,玩什么网络游戏赚钱,吃醋的kk

某次需要使用C#对注册表进行操作,不过却发现没有权限,研究了以下发现是当前系统用户的问题。除非当前系统用户是Administrator,否则就会给你抛出一个异常。后来在网上发现了一个方法,原来C#也可以获取用户的系统管理员权限的,虽然需要用户进行确认。

这里我对Oracle软件的一个键进行了操作,想要将值改为一个指定的字符。在进入注册表后,进行提升权限操作,获取权限后执行bat文件和reg文件。当然,如果不想用bat和reg文件也可以,不过那就需要在操作注册表之前进行提升权限操作了。

            try
            {
                //操作注册表进入指定键值对
                Microsoft.Win32.RegistryKey RegistryRoot = Microsoft.Win32.Registry.LocalMachine;
                string[] path = new string[] { "SOFTWARE", "ORACLE", "KEY_OraDb10g_home1" };
                foreach (string p in path)
                {
                    if (RegistryRoot != null)
                        RegistryRoot = RegistryRoot.OpenSubKey(p);
                }
                //判断键值对是否为空
                if (RegistryRoot != null)
                {
                    object value = RegistryRoot.GetValue("NLS_LANG");
                    //如果键值不符合标准,则修改注册表键值对
                    if (value == null || value.ToString().ToUpper() != "ZHS16GBK")
                    {
                        System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                        System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                        //判断当前登录用户是否为管理员
                        if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
                        {
                            //如果是管理员,则直接运行
                            System.Diagnostics.Process.Start("regedit.exe", "/s oracle_char.reg");
                        }
                        else
                        {
                            //创建启动对象
                            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                            startInfo.UseShellExecute = true;
                            startInfo.WorkingDirectory = Environment.CurrentDirectory;
                            //startInfo.FileName = Application.ExecutablePath;
                            startInfo.FileName = string.Format("{0}oracle_char.bat", AppDomain.CurrentDomain.BaseDirectory);
                            //设置启动动作,确保以管理员身份运行
                            startInfo.Verb = "runas";
                            System.Diagnostics.Process.Start(startInfo);
                        }
                    }
                }
            }
            catch
            {

            }

bat文件:

@echo off  
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit  
cd /d "%~dp0" 
@FOR %%A IN (oracle_char.reg) DO (REGEDIT /S %%A)

reg文件:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraDb10g_home1]
"NLS_LANG"="ZHS16GBK"

在运行提升权限操作时,程序会弹出一个窗口,要求用户提升权限。Adminsitrator用户有密码的话输入密码,没有密码点击一下确认按钮就可以了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网