http://www.csdn.net/Develop/read_article.asp?id=25900
解决方案 »
- 连接数据库
- C# 多线程 及 线程内部同步委托问题
- ASP.net WEB里怎么弹出对话框啊?
- C#程序启动时,连接到指定的服务器判断是否更新
- 有关this.close()的问题
- C#里String赋值次数多了是否老出毛病?怎么回事
- 請問winform中如何遍歷datagrid並為datagrid中每一行的ComboBox的DataSource設置不同的值?
- 怎样在 datable 中取得嵌套的 xml 内容,请大家帮忙
- 散分,谁知道怎么弄到特别号码?
- 能不能在保存文件对话框的返回文件路径加上在网络上的绝对路径?
- 为什么我这样在原有数据表中(执行SQL命令)插入数据回提示:“未将对象引用设置到对象实例”?
- Whidbey Winbar控件
/// 执行单条命令
/// </summary>
/// <param name="commandText">命令文本</param>
/// <returns>命令输出文本</returns>
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
//string formats = s.Replace("\r","").Replace("\n","\r\n");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
/// <summary>
/// 执行多条命令
/// </summary>
/// <param name="commandArray">命令文本数组</param>
/// <returns>命令输出文本</returns>
public static string ExeCommand(string [] commandTexts)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
foreach(string item in commandTexts)
{
p.StandardInput.WriteLine(item);
}
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
最好用System.Environment.GetEnvironmentVariable("comspec")来获得cmd.exe或command.com的路径。