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\r");
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}\nExitCode:{1}\nOutputString:{2}\nMoreOutputString:\n", Success, ExitCode, OutputString);
119 if (MoreOutputString != null)
120 foreach (var v in MoreOutputString)
121 str.AppendFormat("{0}:{1}\n", v.Key, v.Value.Replace("\r", "\\Ⓡ").Replace("\n", "\\Ⓝ"));
122 return str.ToString();
123 }
124 }
125 }
126 }