This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
138 lines (118 loc) · 4.62 KB
/
Program.cs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace dotnetCampus.UpdateAllDotNetTools
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting update all dotnet tools");
Console.WriteLine("Finding installed tools");
foreach (var temp in Parse(Command("dotnet tool list -g")))
{
UpdateTool(temp);
}
Console.WriteLine("Update finished");
}
private static void UpdateTool(string tool)
{
Console.WriteLine(Command($"dotnet tool update {tool} -g"));
}
private static IEnumerable<string> Parse(string command)
{
Console.WriteLine(command);
var stringReader = new StringReader(command);
var line = stringReader.ReadLine();
while (line != null)
{
if (line.Contains("-------------------------"))
{
break;
}
line = stringReader.ReadLine();
}
line = stringReader.ReadLine();
var regex = new Regex(@"(\S+)\s+", RegexOptions.Compiled);
while (line != null)
{
var match = regex.Match(line);
yield return match.Groups[1].Value;
line = stringReader.ReadLine();
}
}
private static string Command(string str, string workingDirectory = null)
{
Console.WriteLine(str);
if (string.IsNullOrEmpty(workingDirectory))
{
workingDirectory = Environment.CurrentDirectory;
}
var p = new Process
{
StartInfo =
{
FileName = "cmd.exe",
WorkingDirectory = workingDirectory,
UseShellExecute = false, //是否使用操作系统shell启动
RedirectStandardInput = true, //接受来自调用程序的输入信息
RedirectStandardOutput = true, //由调用程序获取输出信息
RedirectStandardError = true, //重定向标准错误输出
CreateNoWindow = true, //不显示程序窗口
//StandardOutputEncoding = Encoding.GetEncoding("GBK") //Encoding.UTF8
//Encoding.GetEncoding("GBK");//乱码
}
};
p.Start(); //启动程序
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(str + "&exit");
p.StandardInput.AutoFlush = true;
//p.StandardInput.WriteLine("exit");
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
bool exited = false;
//// 超时
//Task.Run(() =>
//{
// Task.Delay(TimeSpan.FromMinutes(1)).ContinueWith(_ =>
// {
// if (exited)
// {
// return;
// }
// try
// {
// if (!p.HasExited)
// {
// Console.WriteLine($"{str} 超时");
// p.Kill();
// }
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// }
// });
//});
//获取cmd窗口的输出信息
string output = p.StandardOutput.ReadToEnd();
//Console.WriteLine(output);
output += p.StandardError.ReadToEnd();
//Console.WriteLine(output);
//StreamReader reader = p.StandardOutput;
//string line=reader.ReadLine();
//while (!reader.EndOfStream)
//{
// str += line + " ";
// line = reader.ReadLine();
//}
p.WaitForExit(TimeSpan.FromMinutes(1).Milliseconds); //等待程序执行完退出进程
p.Close();
exited = true;
return output + "\r\n";
}
}
}