如何逐个运行一些CMD命令(作为管理员)

时间:2017-08-27 21:20:38

标签: c# cmd command

我想通过CMD(管理员)运行一些命令: ipconfig/flushdns ipconfig /registerdns ipconfig /release ipconfig /renew netsh winsock reset 谁能告诉我怎么样?

2 个答案:

答案 0 :(得分:0)

如果您希望使用PowerShell,可以创建一个脚本,其中包含以下命令:

& "command 1"; & "command 2";

答案 1 :(得分:0)

问题在这里排序: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();