我是靠谱客的博主 魁梧白开水,这篇文章主要介绍c# 调用 执行 dos 命令,现在分享给大家,希望可以做个参考。

复制代码
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System; using System.Collections.Generic; using System.Text; using System.Management; using System.Diagnostics; using System.Windows.Forms; using System.IO; namespace serviceConfig { public class CmdProcess { #region 导出 public static string exportData(string businessType) { if (businessType=="singleProcess")//单进程导出 { string strCmd = ""; strCmd = "singleProcess.bat"; string strOutPut = ExecuteDosCmd(strCmd, 30000); return strOutPut; } else//多进程导出 { string strCmd = ""; strCmd = "exportFromWeb " + businessType; string strOutPut = ExecuteDosCmd(strCmd, 30000); return strOutPut; } } #endregion #region 导入 public static string importData(string businessType) { string strCmd = ""; strCmd = "import " + businessType; ; string strOutPut = ExecuteDosCmd(strCmd, 30000); return strOutPut; } #endregion #region 注册服务 public static string registerService() { string strCmd = ""; strCmd = "instsrv exportToolService exportTool "; string strOutPut = ExecuteDosCmd(strCmd, 30000); return strOutPut; } #endregion #region 写log public static void WriteLogToFile(string writeString) { string strDic = System.AppDomain.CurrentDomain.BaseDirectory; IniMen readIdent = new IniMen(); readIdent.LoadFromFile(strDic + "service.ini"); string logFile = readIdent.ReadIdent("configSet", "selectLogFilePath", ""); FileStream fs = null; System.DateTime currentTime = System.DateTime.Now; string filePath = logFile + "\CMD_LOG_" + currentTime.ToLongDateString() + ".txt"; try { if (false == System.IO.Directory.Exists(logFile)) { //创建文件夹 System.IO.Directory.CreateDirectory(logFile); } if (File.Exists(filePath)) { fs = new FileStream(filePath, FileMode.Open); } else { fs = new FileStream(filePath, FileMode.Create); } } catch (Exception e) { e.ToString(); } fs.Seek(0, SeekOrigin.End); StreamWriter sw = new StreamWriter(fs); sw.Write("["); sw.Write(currentTime.ToString()); sw.Write("]"); sw.Write(writeString); sw.Write("rn"); sw.Flush(); sw.Close(); fs.Close(); } #endregion #region 执行DOS命令,等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待,返回DOS命令的输出,如果发生异常,返回空字符串 public static string ExecuteDosCmd(string dosCommand, int milliseconds) { //输出字符串 string output = ""; if (dosCommand != null && dosCommand != "") { //创建进程对象 Process process = new Process(); //指定执行路径,开机自己启动时不指定路径,cmd异常退出 process.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory; //设定需要执行的命令 process.StartInfo.FileName = "cmd.exe"; //不使用系统外壳程序启动 process.StartInfo.UseShellExecute = false; //不重定向输入 process.StartInfo.RedirectStandardInput = true; //重定向输出,而不是默认的显示在dos控制台 process.StartInfo.RedirectStandardOutput = true; //输出错误信息 process.StartInfo.RedirectStandardError = true; //不创建窗口 process.StartInfo.CreateNoWindow = true; //设定参数,其中的“/C”表示执行完命令后马上退出 process.StartInfo.Arguments = "/C " + dosCommand; try { //开始进程 if (process.Start()) { if (milliseconds == 0) { //这里无限等待进程结束 process.WaitForExit(); } else { //这里等待进程结束,等待时间为指定的毫秒 process.WaitForExit(milliseconds); } //读取进程的输出结果。 output = process.StandardOutput.ReadToEnd(); if (process.ExitCode == 0) { return "export success"; } else { //WriteLogToFile("export 命令执行失败 process.ExitCode= "+ process.ExitCode); return "export Error"; } } } catch (Exception ex) { //WriteLogToFile("export 命令执行失败 错误消息:" + ex.Message); output = ex.Message; } finally { if (process != null) { process.Close(); process.Dispose(); } } } return output; } #endregion public static string ExecuteCmd(string dosCommand, int milliseconds) { // 用ProcessStartInfo启动一个exe程序 ProcessStartInfo start = new ProcessStartInfo(); start.FileName = dosCommand; //exe程序的路径 start.UseShellExecute = false; start.RedirectStandardOutput = true; //启动调用 using (Process process = Process.Start(start)) { WriteLogToFile("ExecuteCmd 开始"); process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived ); process.BeginOutputReadLine(); //等待退出 process.WaitForExit(); WriteLogToFile("ExecuteCmd 结束"); } return "export success"; } //委托接受exe的输出内容 static void process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (null != e) { Console.WriteLine(e.Data); WriteLogToFile("process_OutputDataReceived " + e.Data); } } } }

最后

以上就是魁梧白开水最近收集整理的关于c# 调用 执行 dos 命令的全部内容,更多相关c#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部