-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunCommand.cs
147 lines (141 loc) · 5.25 KB
/
RunCommand.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
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LckeyAnt
{
public class RunCommand
{
#region run outin command
/// <summary>
/// 调用bat,传入参数args,只能使用传入的参数执行一次
/// </summary>
/// <param name="fileName"></param>
/// <param name="args"></param>
public static void callBat(string fileName, string args) {
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.EnableRaisingEvents = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;//true
//p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
// @"C:\Documents and Settings\thomas\桌面\test\t.bat";
LogOutput.logConsoleNow(fileName);
p.StartInfo.FileName = fileName;
//p.StartInfo.WorkingDirectory = @"E:\";
p.StartInfo.Arguments = args;//传入bat的参数
p.StartInfo.LoadUserProfile = false;
p.Start();
string ret = p.StandardOutput.ReadToEnd(); //获取返回值
LogOutput.logConsoleNow(ret);
p.WaitForExit();
p.Close();
}
/**
*http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx
*http://forum.codecall.net/topic/49702-running-command-line-with-processstart/
*
*如:Process.Start("IExplore.exe", "www.northwindtraders.com");
* Process.Start("ftp.exe","xx.xx.xx.xxx");
* Process.Start("cmd.exe","/copy xxx");
*/
/// <summary>
/// 显示cmd窗口自己调用
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="args"></param>
public static void callSysExe(string fileName, string args) {
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = false;//true
//p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
//System.Diagnostics.Process.Start("IExplore.exe", "www.baidu.com");
//直接使用命令则是默认调用cmd.exe
//System.Diagnostics.Process.Start("ipconfig");
System.Diagnostics.Process.Start(fileName, args);
}
/// <summary>
/// 使用cmd环境执行命令,同步执行一句传入命令
/// </summary>
/// http://www.cnblogs.com/wucg/archive/2012/03/16/2399980.html
/// <param name="workDir"></param>
/// <param name="cmdStr"></param>
public static void callCmdSync(string workDir, string cmdStr) {
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.EnableRaisingEvents = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;//true
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe"; //fileName;
p.StartInfo.WorkingDirectory = workDir;//@"E:\";
//p.StartInfo.Arguments = args;//传入bat的参数
p.StartInfo.LoadUserProfile = false;
p.Start();
//输出到命令行
p.StandardInput.WriteLine(cmdStr);
p.StandardInput.WriteLine("exit");
//捕获不到类似 dirxxx的错误信息
//string ret = p.StandardOutput.ReadToEnd(); //获取返回值
//LogOutput.logConsoleNow(ret);
/* 按行获取返回值 */
string line = p.StandardOutput.ReadLine();//每次读取一行
while (!p.StandardOutput.EndOfStream) {
if (line != string.Empty) {
LogOutput.logConsoleNow(line + " ");
}
line = p.StandardOutput.ReadLine();
}
p.WaitForExit();
p.Close();
LogOutput.logConsoleNow("--cmd over--");
}
/// <summary>
/// 使用cmd环境执行命令,同步执行一句传入命令
/// </summary>
/// http://www.cnblogs.com/wucg/archive/2012/03/16/2399980.html
/// <param name="workDir"></param>
/// <param name="cmdStr"></param>
public static void callCmdSync(string workDir, List<string> cmdList) {
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.EnableRaisingEvents = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;//true
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe"; //fileName;
p.StartInfo.WorkingDirectory = workDir;//@"E:\";
//p.StartInfo.Arguments = args;//传入bat的参数
p.StartInfo.LoadUserProfile = false;
p.Start();
LogOutput.logConsole("pause 会被主动过滤掉,会引起下面传入参数错误");
//输出到命令行
foreach (string cmdStr in cmdList) {
LogOutput.logConsole(cmdStr);
//过滤掉pause情况,会引起下面传入错误
if (cmdStr.ToLower() != "pause") {
p.StandardInput.WriteLine(cmdStr);
p.StandardInput.Flush();
}
}
p.StandardInput.WriteLine("exit");
//捕获不到类似 dirxxx的错误信息
//string ret = p.StandardOutput.ReadToEnd(); //获取返回值
//LogOutput.logConsoleNow(ret);
/* 按行获取返回值 */
string line = p.StandardOutput.ReadLine();//每次读取一行
while (!p.StandardOutput.EndOfStream) {
if (line != string.Empty) {
LogOutput.logConsoleNow(line + " ");
}
line = p.StandardOutput.ReadLine();
}
p.WaitForExit();
p.Close();
LogOutput.logConsoleNow("--cmd over--");
}
#endregion
}
}