如何逐个运行一些CMD命令(作为管理员)
问题描述:
我想通过CMD运行几个命令(管理员): ipconfig/flushdns ipconfig /registerdns ipconfig /release ipconfig /renew netsh winsock reset
任何人都可以告诉我怎么做?如何逐个运行一些CMD命令(作为管理员)
答
这里排序回答:Running cmd commands with Administrator rights
代码可以复制(如下所示):
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ipconfig /flushdns";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo2 = new
System.Diagnostics.ProcessStartInfo();
startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo2.FileName = "cmd.exe";
startInfo2.Arguments = "/C ipconfig /renew";
startInfo2.Verb = "runas";
process2.StartInfo = startInfo2;
process2.Start();
答
如果您正在寻找使用PowerShell,你可以创建一个脚本,其中将包括你的命令是这样的:
& "command 1";
& "command 2";
等问题的
[以编程方式提升进程权限?](https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically) – Vanna