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的更多相关文章

  1. Delphi---ShellExecute跨进程调用exe

    测试环境:Delphi7 + Win7 发起端 unit uRequest; interface uses Windows, Messages, SysUtils, Variants, Classes ...

  2. 使用java传参调用exe并且获取程序进度和返回结果的一种方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 在某个项目中需要考虑使用java后台调用由C#编写的切图程序( ...

  3. C#调用exe文件,IIS发布后无法掉用本地exe程序的解决方法

    http://blog.csdn.net/junjieking/article/details/6277836?reload这位楼主的问题,我也遇到了,但是我按照他那样操作并没有解决问题,弄了好久终于 ...

  4. C#调用Exe文件的方法及如何判断程序调用的exe已结束

    很简单的代码就可以实现C#调用EXE文件,如下: 引入using System.Diagnostics; 调用代码: Process.Start(exe文件名); 或直接 System.Diagnos ...

  5. 64位进程调用32位dll的解决方法 / 程序64位化带来的问题和思考

    最近做在Windows XP X64,VS2005环境下做32位程序编译为64位程序的工作,遇到了一些64位编程中可能遇到的问题:如内联汇编(解决方法改为C/C++代码),long类型的变化,最关键的 ...

  6. C#调用EXE

    1.问题意义 据说界面程序开发,首选C#(像lebview之类的也很好) 但是,能不能用其他语言开发核心代码,只用C#做界面?毕竟每种语言都有自己擅长的领域. 2.exe程序 比如有个example. ...

  7. 64位进程调用32位dll的解决方法

    64位进程调用32位dll的解决方法   最近做在Windows XP X64,VS2005环境下做32位程序编译为64位程序的工作,遇到了一些64位编程中可能遇到的问题:如内联汇编(解决方法改为C/ ...

  8. ABP框架 - 介绍 VS2017调试器无法附加到IIS进程(w3wp.exe) c# 动态实例化一个泛型类

    ABP框架 - 介绍   在14,15年间带领几个不同的团队,交付了几个项目,在这个过程中,虽然几个项目的业务不一样,但是很多应用程序架构基础性的功能却是大同小异,例如认证.授权.请求验证.异常处理. ...

  9. C#调用Exe程序示例

    在编写程序时经常会使用到调用可执行程序的情况,本文将简单介绍C#调用exe的方法.在C#中,通过Process类来进行进程操作. Process类在System.Diagnostics包中. 示例一 ...

随机推荐

  1. 分布式事务之——tcc-transaction分布式TCC型事务框架搭建与实战案例(基于Dubbo/Dubbox)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/73731363 一.背景 有一定分布式开发经验的朋友都知道,产品/项目/系统最初为了 ...

  2. 【Android实验】 数据存储与访问sqlite

    目录 实验目的 实验要求 实验过程 功能分析: 实验结果: 实验的代码 实验总结 实验目的     分别使用sqlite3工具和Android代码的方式建立SQLite数据库.在完成建立数据库的工作后 ...

  3. NS3 一个小问题

    可能会在执行./waf 命令的时候遇到这个问题,比如我想编译 /home/wasdns/Documents/NS3/ns-3.17/scratch 目录下的一个文件:newnsthree.cpp 编译 ...

  4. HDU 2242 考研路茫茫——空调教室(边双连通分量+树形dp+重边标号)

    http://acm.hdu.edu.cn/showproblem.php?pid=2242 题意: 思路:首先求一下双连通分量,如果只有一个双连通分量,那么无论断哪根管子,图还是连通的. 最后只需要 ...

  5. C# 获取文件夹下的所有文件夹及其文件

    //获得当前文件夹下所有文件夹 string path = "D://文件夹"; string[] dirstr = Directory.GetDirectories(path); ...

  6. go 异常处理

    package main import "fmt" func main() { defer func() { if err := recover(); err != nil { f ...

  7. editplus5激活码

    editplus5激活码 name: Vovan code: 3AG46-JJ48E-CEACC-8E6EW-ECUAW 转自:https://blog.csdn.net/webfront/artic ...

  8. JS 字符串 作为变量名

    function initCKEditor(querySelector,content_val,myEditor) { ClassicEditor.create(document.querySelec ...

  9. JavaScript运算符与类型

    1.运算符优先级 首先讲一下运算符的优先级,它决定了表达式中运算执行的先后顺序,优先级高的运算符最先被执行. 下面的表将所有运算符按照优先级的不同从高到低排列: 优先级 运算类型 关联性 运算符 19 ...

  10. Python -- Scrapy 架构概览

    架构概览 本文档介绍了Scrapy架构及其组件之间的交互. 概述 接下来的图表展现了Scrapy的架构,包括组件及在系统中发生的数据流的概览(绿色箭头所示). 下面对每个组件都做了简单介绍,并给出了详 ...