zoukankan      html  css  js  c++  java
  • c# 进程调用exe

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Diagnostics;
      5 using System.Text;
      6 using System.Threading;
      7 
      8 namespace AliPayaDyncForm.Common
      9 {
     10     public class ProcessHelper
     11     {
     12         private static Process GetProcess()
     13         {
     14             Process mProcess = new Process();
     15             mProcess.StartInfo.CreateNoWindow = true;
     16             mProcess.StartInfo.UseShellExecute = false;
     17             mProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
     18 
     19             mProcess.StartInfo.RedirectStandardInput = true;
     20             mProcess.StartInfo.RedirectStandardError = true;
     21             mProcess.StartInfo.RedirectStandardOutput = true;
     22             mProcess.StartInfo.StandardOutputEncoding = Encoding.UTF8;
     23 
     24             return mProcess;
     25         }
     26 
     27         /// <summary>
     28         /// 读取数据的时候等待时间,等待时间过短时,可能导致读取不出正确的数据。
     29         /// </summary>
     30         public static int WaitTime = 50;
     31         
     32         public static RunResult Run(string exePath, string args)
     33         {
     34             var result = new RunResult();
     35             using (var p = GetProcess())
     36             {
     37                 try
     38                 {
     39                     p.StartInfo.FileName = exePath;
     40                     p.StartInfo.Arguments = args;
     41                     p.Start();
     42 
     43                     //获取正常信息
     44                     if (p.StandardOutput.Peek() > -1)
     45                         result.OutputString = p.StandardOutput.ReadToEnd();
     46 
     47                     //获取错误信息
     48                     if (p.StandardError.Peek() > -1)
     49                         result.OutputString = p.StandardError.ReadToEnd();
     50 
     51                     Thread.Sleep(WaitTime);
     52                     p.StandardInput.WriteLine("exit
    ");
     53                     //等待结束
     54                     p.WaitForExit(WaitTime);
     55 
     56                     result.ExitCode = p.ExitCode;
     57                     result.Success = true;
     58 
     59                 }
     60                 catch (Win32Exception ex)
     61                 {
     62                     result.Success = false;
     63 
     64                     //System Error Codes (Windows)
     65                     //http://msdn.microsoft.com/en-us/library/ms681382(v=vs.85).aspx
     66                     result.OutputString = string.Format("{0},{1}", ex.NativeErrorCode,
     67                         SystemErrorCodes.ToString(ex.NativeErrorCode));
     68                     Console.Write($"{ex.NativeErrorCode},{SystemErrorCodes.ToString(ex.NativeErrorCode)}");
     69                 }
     70                 catch (Exception ex)
     71                 {
     72                     result.Success = false;
     73                     result.OutputString = ex.ToString();
     74                 }
     75                 finally
     76                 {
     77                     try
     78                     {
     79                         if (!p.HasExited)
     80                         {
     81                             p.Kill();
     82                         }
     83                     }
     84                     catch (Exception ex)
     85                     {
     86                         result.MoreOutputString.Add(99999, ex.ToString());
     87                     }
     88                 }
     89                 return result;
     90             }
     91         }
     92 
     93         public class RunResult
     94         {
     95             public RunResult()
     96             {
     97                 OutputString = string.Empty;
     98                 MoreOutputString = new Dictionary<int, string>();
     99             }
    100 
    101             /// <summary>
    102             /// 当执行不成功时,OutputString会输出错误信息。
    103             /// </summary>
    104             public bool Success;
    105             public int ExitCode;
    106             public string OutputString;
    107 
    108             /// <summary>
    109             /// 调用RunAsContinueMode时,使用额外参数的顺序作为索引。
    110             /// 如:调用ProcessHelper.RunAsContinueMode(AdbExePath, "shell", new[] { "su", "ls /data/data", "exit", "exit" });
    111             /// 果:MoreOutputString[0] = su执行后的结果字符串;MoreOutputString[1] = ls ...执行后的结果字符串;MoreOutputString[2] = exit执行后的结果字符串
    112             /// </summary>
    113             public Dictionary<int, string> MoreOutputString;
    114 
    115             public new string ToString()
    116             {
    117                 var str = new StringBuilder();
    118                 str.AppendFormat("Success:{0}
    ExitCode:{1}
    OutputString:{2}
    MoreOutputString:
    ", Success, ExitCode, OutputString);
    119                 if (MoreOutputString != null)
    120                     foreach (var v in MoreOutputString)
    121                         str.AppendFormat("{0}:{1}
    ", v.Key, v.Value.Replace("
    ", "\Ⓡ").Replace("
    ", "\Ⓝ"));
    122                 return str.ToString();
    123             }
    124         }
    125     }
    126 }
  • 相关阅读:
    中文字体在CSS中的表达方式
    图片上传+预览+剪切解决方案我们到底能走多远系列(20)
    C# — 饼形图之插入介绍文字
    CSS 网页布局中文排版的9则技巧
    Android UI 优化 使用<include/>和 <merge />标签
    SQLite 的日期时间函数
    GSM、GPRS、EDGE、2G、3G与WAP的关系
    WPF中的Style(风格,样式)
    给力分享新的ORM => Dapper
    WCF开发框架形成之旅如何实现X509证书加密
  • 原文地址:https://www.cnblogs.com/password1/p/9455375.html
Copyright © 2011-2022 走看看