process执行DOS命令
2008-12-19 15:37
399 查看
使用Process执行cmd命令 netstat -an 。然后分析输出结果就行了。
C# code
C# code
/// <summary> /// 执行DOS命令,返回DOS命令的输出 /// </summary> /// <param name="dosCommand">dos命令</param> /// <param name="milliseconds">等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待</param> /// <returns>返回输出,如果发生异常,返回空字符串</returns> public static string Execute(string dosCommand, int milliseconds) { string output = ""; //输出字符串 if (dosCommand != null && dosCommand != "") { Process process = new Process(); //创建进程对象 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; //设定需要执行的命令 startInfo.Arguments = "/C " + dosCommand; //设定参数,其中的“/C”表示执行完命令后马上退出 startInfo.UseShellExecute = false; //不使用系统外壳程序启动 startInfo.RedirectStandardInput = false; //不重定向输入 startInfo.RedirectStandardOutput = true; //重定向输出 startInfo.CreateNoWindow = true; //不创建窗口 process.StartInfo = startInfo; try { if (process.Start()) //开始进程 { if (milliseconds == 0) process.WaitForExit(); //这里无限等待进程结束 else process.WaitForExit(milliseconds); //这里等待进程结束,等待时间为指定的毫秒 output = process.StandardOutput.ReadToEnd();//读取进程的输出 } } catch { } finally { if (process != null) process.Close(); } } return output; }
相关文章推荐
- Process执行dos命令
- 7.使用ProcessBuilder执行本地命令(转)
- Process 执行Linux命令/调用外部程序
- 执行dos命令而不显示界面
- 用asp.net调用dos命令~(通过cmd来执行)---转载的
- C#中执行dos命令,返回结果值
- 在JSP中执行dos命令(转载)
- C#执行DOS命令,返回DOS命令的输出
- 在VB中调用DOS命令,如何得知DOS命令是否已经执行完毕
- C#中一种执行命令行或DOS内部命令的方法
- DOS下通过批处理执行sql命令
- [整理修改] DOS 批处理命令判断操作系统版本 执行各版本对应语句
- 执行Hbase命令遇到了错误: regionserver running as process ×××. Stop it first.解决办法
- 如何在DOS下面执行相关的命令
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 【原创】DOS命令下执行java文件方法
- python中subprocess.Popen执行命令并持续获取返回值
- dos下将命令的执行结果赋给变量
- [收集]用asp.net调用dos命令~(通过cmd来执行)