C# 直接调用DOS命令,窗口隐藏,并直接返回命令执行结果
public string RunCmd(string cmd) { Process proc = new Process(); proc.StartInfo.CreateNoWindow = true; proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.Arguments = "/C " + cmd; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); //proc.StandardInput.WriteLine(cmd); //proc.StandardInput.WriteLine("exit"); string outStr = proc.StandardOutput.ReadToEnd(); proc.Close(); return outStr; }
常用DOS命令备忘
获取mac地址 getmac
获取IP配置 ipconfig
获取系统信息 systeminfo
获取打开端口 netstat -ano
获取CPU信息 wmic CPU get ProcessorID
获取所有进程 tasklist
杀死指定进程 taskkill
关机或重启 shutdown