添加自启动程序方法
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;
}
}