c# 进程调用exe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Threading; namespace AliPayaDyncForm.Common
{
public class ProcessHelper
{
private static Process GetProcess()
{
Process mProcess = new Process();
mProcess.StartInfo.CreateNoWindow = true;
mProcess.StartInfo.UseShellExecute = false;
mProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; mProcess.StartInfo.RedirectStandardInput = true;
mProcess.StartInfo.RedirectStandardError = true;
mProcess.StartInfo.RedirectStandardOutput = true;
mProcess.StartInfo.StandardOutputEncoding = Encoding.UTF8; return mProcess;
} /// <summary>
/// 读取数据的时候等待时间,等待时间过短时,可能导致读取不出正确的数据。
/// </summary>
public static int WaitTime = ; public static RunResult Run(string exePath, string args)
{
var result = new RunResult();
using (var p = GetProcess())
{
try
{
p.StartInfo.FileName = exePath;
p.StartInfo.Arguments = args;
p.Start(); //获取正常信息
if (p.StandardOutput.Peek() > -)
result.OutputString = p.StandardOutput.ReadToEnd(); //获取错误信息
if (p.StandardError.Peek() > -)
result.OutputString = p.StandardError.ReadToEnd(); Thread.Sleep(WaitTime);
p.StandardInput.WriteLine("exit\r");
//等待结束
p.WaitForExit(WaitTime); result.ExitCode = p.ExitCode;
result.Success = true; }
catch (Win32Exception ex)
{
result.Success = false; //System Error Codes (Windows)
//http://msdn.microsoft.com/en-us/library/ms681382(v=vs.85).aspx
result.OutputString = string.Format("{0},{1}", ex.NativeErrorCode,
SystemErrorCodes.ToString(ex.NativeErrorCode));
Console.Write($"{ex.NativeErrorCode},{SystemErrorCodes.ToString(ex.NativeErrorCode)}");
}
catch (Exception ex)
{
result.Success = false;
result.OutputString = ex.ToString();
}
finally
{
try
{
if (!p.HasExited)
{
p.Kill();
}
}
catch (Exception ex)
{
result.MoreOutputString.Add(, ex.ToString());
}
}
return result;
}
} public class RunResult
{
public RunResult()
{
OutputString = string.Empty;
MoreOutputString = new Dictionary<int, string>();
} /// <summary>
/// 当执行不成功时,OutputString会输出错误信息。
/// </summary>
public bool Success;
public int ExitCode;
public string OutputString; /// <summary>
/// 调用RunAsContinueMode时,使用额外参数的顺序作为索引。
/// 如:调用ProcessHelper.RunAsContinueMode(AdbExePath, "shell", new[] { "su", "ls /data/data", "exit", "exit" });
/// 果:MoreOutputString[0] = su执行后的结果字符串;MoreOutputString[1] = ls ...执行后的结果字符串;MoreOutputString[2] = exit执行后的结果字符串
/// </summary>
public Dictionary<int, string> MoreOutputString; public new string ToString()
{
var str = new StringBuilder();
str.AppendFormat("Success:{0}\nExitCode:{1}\nOutputString:{2}\nMoreOutputString:\n", Success, ExitCode, OutputString);
if (MoreOutputString != null)
foreach (var v in MoreOutputString)
str.AppendFormat("{0}:{1}\n", v.Key, v.Value.Replace("\r", "\\Ⓡ").Replace("\n", "\\Ⓝ"));
return str.ToString();
}
}
}
}
c# 进程调用exe的更多相关文章
- Delphi---ShellExecute跨进程调用exe
测试环境:Delphi7 + Win7 发起端 unit uRequest; interface uses Windows, Messages, SysUtils, Variants, Classes ...
- 使用java传参调用exe并且获取程序进度和返回结果的一种方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 在某个项目中需要考虑使用java后台调用由C#编写的切图程序( ...
- C#调用exe文件,IIS发布后无法掉用本地exe程序的解决方法
http://blog.csdn.net/junjieking/article/details/6277836?reload这位楼主的问题,我也遇到了,但是我按照他那样操作并没有解决问题,弄了好久终于 ...
- C#调用Exe文件的方法及如何判断程序调用的exe已结束
很简单的代码就可以实现C#调用EXE文件,如下: 引入using System.Diagnostics; 调用代码: Process.Start(exe文件名); 或直接 System.Diagnos ...
- 64位进程调用32位dll的解决方法 / 程序64位化带来的问题和思考
最近做在Windows XP X64,VS2005环境下做32位程序编译为64位程序的工作,遇到了一些64位编程中可能遇到的问题:如内联汇编(解决方法改为C/C++代码),long类型的变化,最关键的 ...
- C#调用EXE
1.问题意义 据说界面程序开发,首选C#(像lebview之类的也很好) 但是,能不能用其他语言开发核心代码,只用C#做界面?毕竟每种语言都有自己擅长的领域. 2.exe程序 比如有个example. ...
- 64位进程调用32位dll的解决方法
64位进程调用32位dll的解决方法 最近做在Windows XP X64,VS2005环境下做32位程序编译为64位程序的工作,遇到了一些64位编程中可能遇到的问题:如内联汇编(解决方法改为C/ ...
- ABP框架 - 介绍 VS2017调试器无法附加到IIS进程(w3wp.exe) c# 动态实例化一个泛型类
ABP框架 - 介绍 在14,15年间带领几个不同的团队,交付了几个项目,在这个过程中,虽然几个项目的业务不一样,但是很多应用程序架构基础性的功能却是大同小异,例如认证.授权.请求验证.异常处理. ...
- C#调用Exe程序示例
在编写程序时经常会使用到调用可执行程序的情况,本文将简单介绍C#调用exe的方法.在C#中,通过Process类来进行进程操作. Process类在System.Diagnostics包中. 示例一 ...
随机推荐
- hdu 1788 最小公倍数(这题面。。。)
Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- WPF基础学习笔记整理 (二) XAML
基础知识: XAML:Extensible Application Markup Language, zammel: 用于实例化.NET对象的标记语言: XMAL使用树形逻辑结构描述UI: BAML: ...
- python 分数的数学四则运算
import fractions f1 = fractions.Fraction(, ) f2 = fractions.Fraction(, ) print('{} + {} = {}'.format ...
- Could not find a package configuration file provided by 'ecl_geometry' ,.................couldn't find required component 'ecl_geometry'
sudo apt-get install ros-kinetic-ecl-geometry
- resource not found :rgbd_launch
放到src下,再次编译catkin_make git https://github.com/ros-drivers/rgbd_launch.git
- django模型和字段
一个模型(model)就是一个单独的.确定的数据的信息源,包含了数据的字段和操作方法.通常,每个模型映射为一张数据库中的表. 基本的原则如下: 每个模型在Django中的存在形式为一个Python类 ...
- android中 检查网络连接状态的变化,无网络时跳转到设置界面
1:在AndroidManifest.xml中加一个声明 <receiver android:name="NetCheckReceiver"> <inten ...
- php json josn_decode()返回的是对像,如何把对像转成数组
php json josn_decode()返回的是对像,如何把对像转成数组 a.php传值页面,使用 json_encode($array)对数组进行加密码. b.php页面在接收a.php传过来的 ...
- WPF几种高级绑定
(1)Binding + RelativeSource + AncestorType 模式 , 根据关联源所指定的类型,可动态绑定指定类型的Path属性(Path可以省略)(PS:动态指父级在运行 ...
- 解决jenkins运行selenium测试出错的问题
If you run Jenkins as a service in the background it won't open apps in the foreground. You may eith ...