.net 中,使用c# 语言 ,执行exe程序。
来源:互联网 发布:陌游为什么网络异常 编辑:程序博客网 时间:2024/05/16 01:38
最近在操作adb做一些事情,就需要开发个windows引用程序。在这里就需要使用winform来调用adb.exe来做事了。然而,要正确调用,还想要得到调用成功或者的失败的反馈。就需要做点事情了,需要对输出流和异常流进行重定向,其中总是遇到readToEnd方法的假死。查询了一些文档,终于找到一个比较好的实现。代码如下:
/// <summary>
/// 执行exe
/// </summary>
public class ProcessExcuter
{
public static void Run(string exeFilePath, string args, out string res, out string error)
{
if (string.IsNullOrEmpty(exeFilePath) || !System.IO.File.Exists(exeFilePath))
{
throw new System.IO.FileNotFoundException();
}
if (string.IsNullOrEmpty(args))
{
throw new ArgumentException();
}
Process p;
p = new Process();
p.StartInfo.FileName = exeFilePath;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
int time = 5000;
StringBuilder sbOut = new StringBuilder();
StringBuilder sbError = new StringBuilder();
try
{
p.Start();
p.ErrorDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs e)
{
sbError.Append(e.Data+"\r\n");
});
p.OutputDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs e)
{
sbOut.Append(e.Data + "\r\n");
});
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit(time);
}
finally
{
if (p != null)
{
p.Close();
p.Dispose();
p = null;
}
}
res = sbOut.ToString().Trim();
error = sbError.ToString().Trim();
} }
/// 执行exe
/// </summary>
public class ProcessExcuter
{
public static void Run(string exeFilePath, string args, out string res, out string error)
{
if (string.IsNullOrEmpty(exeFilePath) || !System.IO.File.Exists(exeFilePath))
{
throw new System.IO.FileNotFoundException();
}
if (string.IsNullOrEmpty(args))
{
throw new ArgumentException();
}
Process p;
p = new Process();
p.StartInfo.FileName = exeFilePath;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
int time = 5000;
StringBuilder sbOut = new StringBuilder();
StringBuilder sbError = new StringBuilder();
try
{
p.Start();
p.ErrorDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs e)
{
sbError.Append(e.Data+"\r\n");
});
p.OutputDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs e)
{
sbOut.Append(e.Data + "\r\n");
});
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit(time);
}
finally
{
if (p != null)
{
p.Close();
p.Dispose();
p = null;
}
}
res = sbOut.ToString().Trim();
error = sbError.ToString().Trim();
} }
参考: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginerrorreadline.aspx
0 0
- .net 中,使用c# 语言 ,执行exe程序。
- C#程序执行EXE文件
- C#程序安装.net服务(不使用InstallUtil.exe)
- C#程序安装.net服务(不使用InstallUtil.exe)
- C#程序调用cmd.exe执行命令
- ASP.NET中使用C#调用服务器端exe可执行文件
- C#中调用exe程序
- c#中启动exe程序
- asp.net中执行exe应用程序
- asp.net中执行exe应用程序
- asp.net中执行exe应用程序
- C# 将dll打包到exe中有效方式暨.net程序自动更新有效方式
- [转]C#程序安装.net服务(不使用InstallUtil.exe)
- ASP.NET中调用EXE程序
- asp.net 调用本地程序 调用执行exe应用程序
- c#.net程序中使用ffmpeg.exe来处理视频并生成上传视频的截
- 使用C#如何从你的应用程序执行exe文件
- asp.net中执行exe应用程序,终极解决方案。
- android开发(16) 遍历所有的已经安装的应用程序,启动其他的应用程序
- android开发(17) 扫描二维码 - 调用第三方APK来实现二维码扫描,参考自 开源项目zxing
- Java中的private、protected、public和default的区别
- android开发(18) 调用手机的麦克风录音,并播放。
- android开发(19) 调用手机的摄像头录像,并播放。
- .net 中,使用c# 语言 ,执行exe程序。
- android开发(20) 使用adb建立pc和android设备之间的连接。usb连接方式。
- android开发(21)蜂鸣提示音和震动提示的实现。
- android开发(22)使用正则表达式 。从一个字符串中找出数字,多次匹配。
- ------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! ----
- GLib库安装与简析
- java实现简单算法(一)
- android开发(22)使用正则表达式 。从一个字符串中找出数字,多次匹配。
- 小鑫的城堡