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包中. 示例一 ...
随机推荐
- div滚动条时div内容显示一半
本文为博主原创,未经允许不得转载 今天在做页面浏览器适配时,将页面中的一个div进行放大时,出现了滚动条,但滚动条对应div中的 内容只能显示一半. 仔细对应属性样式时,div具有overflow:h ...
- Mybatis配置映射文件中parameterType的用法小结
原创: 在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配 ...
- Source not found :Edit Source Lookup Path 解决方案
作者原创,转载请注明转载地址 在eclipse中用debug调试的时候,出现了以下问题,很是尴尬,经常碰到,所以有必要进行总结一下: 对该问题有两种解决方案, 一种比较文明:解决方法可参考如下网址: ...
- Linux——进程管理简单学习笔记(二)
计划任务: 为什么要设置计划任务 : 实现数据库备份,发送系统通知神马的..... 计划任务的命令: 一: at : 安排作业在某一时刻执行一次 nbatch 安排作业在系统负载不重时执 行一 ...
- [原][osg][osgEarth]关于在OE中使用物理引擎的调研
关于物理引擎旋转的一些整理 参考文档 http://blog.wolfire.com/2010/03/Comparing-ODE-and-Bullet 介绍ODE和bullet的利弊 http://s ...
- Pyhon 日志管理 -- logging
Pyhon 日志管理 -- logging 一直觉得运行程序是能打印日志是一个神奇的事情,不懂日志产生的原理,后来听说Pyhton 有一个logging模块,So,要好好研究一下. 先贴出代码,看看她 ...
- 丑数问题 Ugly Number
2018-07-28 15:30:21 一.判断是否为丑数 问题描述: 问题求解: 所谓丑数,首先得是正数,然后其质数因子只包含了2,3,4,因此我们只需要对当前的数分别除2,3,4直到不能除为止. ...
- m_Orchestrate learning system---三十二、数据库字段判断为空时容易出现问题,如何从根本上解决这个问题
m_Orchestrate learning system---三十二.数据库字段判断为空时容易出现问题,如何从根本上解决这个问题 一.总结 一句话总结:字段禁止为空,设置默认值0即可 禁止 空 默认 ...
- 递推2--过河卒(Noip2002)
递推2--过河卒(Noip2002) 一.心得 写出递推公式就OK了,具体编程还是很简单的 二.题目及分析 过河卒(NOIp2002) [问题描述] 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的 ...
- Linux-Load Average解析
load Average 转自:http://www.blogjava.net/sliverfancy/archive/2013/04/17/397947.html 1.1:什么是Load?什么是Lo ...