-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProcessHelper.cs
More file actions
91 lines (78 loc) · 2.92 KB
/
Copy pathProcessHelper.cs
File metadata and controls
91 lines (78 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
* Description:ProcessHelper
* Author: Chance.Zheng
* Create Time: 2023/3/3 13:55:14
* .Net Version: 6.0
* CLR Version: 4.0.30319.42000
* Copyright (c) NCATest 2018-2023 All Rights Reserved.
*/
namespace CookPopularInstaller.Generate.CommandLine
{
public class ProcessHelper
{
private static StringBuilder _outputData;
private static StringBuilder _errorData;
private static string _command;
public static string CmdHeader { get; private set; }
public static void StartCmd(string argument, out StringBuilder cmdOutput, out bool hasError)
{
_command = argument;
_outputData = new StringBuilder();
_errorData = new StringBuilder();
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.BeginOutputReadLine();
process.OutputDataReceived += Process_OutputDataReceived;
process.BeginErrorReadLine();
process.ErrorDataReceived += Process_ErrorDataReceived;
StreamWriter streamWriter = process.StandardInput;
streamWriter.WriteLine(argument);
streamWriter.Close();
process.WaitForExit();
process.Close();
int index = _outputData.ToString().IndexOf(argument) + argument.Length;
string output = _outputData.ToString().Substring(index, _outputData.ToString().Length - CmdHeader.Length - index - 2).Trim();
cmdOutput = new StringBuilder(output);
if (string.IsNullOrEmpty(_errorData.ToString()))
{
hasError = false;
}
else
{
hasError = true;
cmdOutput.Append(_errorData);
}
}
private static void GetCmdHeader(string data, string argument)
{
if (string.IsNullOrEmpty(CmdHeader) && data.Contains(argument))
CmdHeader = data.Replace(argument, "").Replace("&exit", "");
}
private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
GetCmdHeader(e.Data, _command);
_outputData.AppendLine(e.Data);
}
}
private static void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
_errorData.AppendLine(e.Data);
}
}
}
}