C# 中操作注册表

添加自启动程序方法

  public static void SetAutoRun(string keyName, string filePath)
        {
            using (RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"software\microsoft\windows\currentversion\run", true))
            {
                runKey.SetValue(keyName, filePath);
                runKey.Close();
            }
        }

//关闭UAC
RegistryKey key = Registry.LocalMachine; //HCU就用CurrentUser, 能.出来
RegistryKey changekey = key.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
changekey.SetValue("ConsentPromptBehaviorAdmin", "0", RegistryValueKind.DWord);
changekey.SetValue("EnableLUA", "0", RegistryValueKind.DWord);
changekey.SetValue("PromptOnSecureDesktop", "0", RegistryValueKind.DWord);
key.Close();
 
//显示隐藏文件和后缀名
RegistryKey key = Registry.CurrentUser;
RegistryKey changekey = key.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", true);
changekey.SetValue("Hidden", "1", RegistryValueKind.DWord);
changekey.SetValue("ShowSuperHidden", "1", RegistryValueKind.DWord);
changekey.SetValue("HideFileExt", "0", RegistryValueKind.DWord);
key.Close();  

windows 中常常用到方法

设置桌面图标和设置电脑声音

			//调用桌面图标设置
            using Process deskprocess = Process.Start("rundll32.exe", "shell32.dll,Control_RunDLL desk.cpl,,5");
            deskprocess.WaitForExit(); //等待进程结束

            //调用声音设置
            using Process deskprocess1 = Process.Start("rundll32.exe", "shell32.dll,Control_RunDLL mmsys.cpl @1");
            deskprocess1.WaitForExit();

CMD 命令行控制方法

 private static readonly string _cmdPath = @"C:\Windows\System32\cmd.exe";
        public static string RunCmd(string cmd)
        {
            try
            {
                cmd = cmd.Trim().TrimEnd('&') + "&exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态
                using Process p = new();
                p.StartInfo.FileName = _cmdPath;
                p.StartInfo.UseShellExecute = false;        //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;   //重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;          //不显示程序窗口
                p.Start();//启动程序

                //向cmd窗口写入命令
                p.StandardInput.WriteLine(cmd);
                p.StandardInput.AutoFlush = true;

                //获取cmd窗口的输出信息
                string output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();
                return output;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return string.Empty;
            }

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值