调用系统工具代码

 

调用系统的工具,并可以实现回调处理核心代码:

public static class DOS
{
public static void CMD(
string commandNameOrFileName,
string arguments,
DataReceivedEventHandler callback)
{
try
{
Process process = new Process();
process.StartInfo.FileName = commandNameOrFileName;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += new DataReceivedEventHandler(callback);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.Close();
}
catch (Exception ex)
{
LogHelper.WriteError("调用系统失败", ex);
throw new ArgumentException("打开工具失败!", ex);
}
}
}

posted on 2013-11-12 10:44  荣锋亮  阅读(238)  评论(0编辑  收藏  举报

导航