在C#里,如何执行cmd里的常用dos命令 (转)
http://blogger.org.cn/blog/more.asp?name=nrzj&id=4280
using System;
using System.Diagnostics;
namespace Tipo.Tools.Utility
{
/// <summary>
/// 常用Dos命令操作
/// </summary>
public class DosCommand
{
private Process process=null;
private string _errmsg;
public DosCommand()
{
process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = false;
process.Start();
}
//检测文件或目录是否存在
public string exist(string path)
{
process.StandardInput.WriteLine(string.Format("dir \"{0}\"",path));
process.StandardInput.WriteLine("exit");
_errmsg = process.StandardError.ReadToEnd();
return _errmsg;
}
}
}