C#调用命令行返回参数方法
2015-07-30 13:46
471 查看
public 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;
}
{
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;
}
相关文章推荐
- C# 事件的理解
- C# DataGridView显示行号、行选中
- C# Enumerable 类
- ArcGIS engine C#开发出现未能加载程序集“ESRI.ArcGIS.Controls, Version=10.0.0.0, Culture=neutral, PublicKeyToken=
- C# 多线程 异步加载 窗体
- C#线程间通讯
- C#关于事件的几个好例子
- C# 导出 数据 到Excel
- C#初级知识点整理及VS的简单使用
- C#窗口实现单例模式的方法
- VS2015 新Web项目(C#6)出现CS1617错误的解决
- 为什么C#串口程序在关闭串口时候会死锁
- 接口的显示实现和隐式实现一点笔记
- C#中out和ref之间的区别
- u3d c# 写一个通用倒计时器
- c# 双缓冲 技术与例子(解决应用程序闪烁问题)
- C# GDI+双缓冲技术
- 浅谈C#中的双缓冲
- C#实现12306自动登录的方法
- C# Debug与release之间的一些小差异