本文实例讲述了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
|
/// <summary> /// 调用dos窗口获取相关信息 /// </summary> /// <param name="cmd">如:netstat-ano或者ipconfig</param> /// <returns></returns> static string GetCode( string cmd) { ProcessStartInfo startInfo = new ProcessStartInfo( "cmd.exe" ); startInfo.UseShellExecute = false ; startInfo.CreateNoWindow = true ; //不显示窗口 Process process = new Process(); process.StartInfo = startInfo; process.StartInfo.RedirectStandardInput = true ; process.StartInfo.RedirectStandardOutput = true ; process.Start(); process.StandardInput.WriteLine(cmd); process.StandardInput.WriteLine( "exit" ); string netMessage = process.StandardOutput.ReadToEnd(); process.WaitForExit(); process.Close(); return netMessage; } /// <summary> /// 将获取的信息输出到桌面 /// </summary> static void InvokeCode() { string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + DateTime.Now.ToString( "yyyyMMddHHmmssfff" ) + ".txt" ; System.IO.File.WriteAllText(path, GetCode( "ipconfig" )); } |
希望本文所述对大家的C#程序设计有所帮助。