C#運行cmd命令

using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.Threading;
06 using System.Diagnostics;
07  
08 namespace ConsoleApplication1
09 {
10     class command
11     {
12         public static string startcmd(string command)
13         {
14             string output = "";
15             try
16             {
17  
18                 Process cmd = new Process();
19                 cmd.StartInfo.FileName = command;
20  
21                 cmd.StartInfo.UseShellExecute = false;
22  
23                 cmd.StartInfo.RedirectStandardInput = true;
24                 cmd.StartInfo.RedirectStandardOutput = true;
25  
26                 cmd.StartInfo.CreateNoWindow = true;
27                 cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
28  
29                 cmd.Start();
30  
31                 output = cmd.StandardOutput.ReadToEnd();
32                 Console.WriteLine(output);
33                 cmd.WaitForExit();
34                 cmd.Close();
35             }
36             catch (Exception e)
37             {
38                 Console.WriteLine(e);
39             }
40             return output;
41         }
42         public static string startcmd(string command, string argument)
43         {
44             string output = "";
45             try
46             {
47                 Process cmd = new Process();
48  
49                 cmd.StartInfo.FileName = command;
50                 cmd.StartInfo.Arguments = argument;
51  
52                 cmd.StartInfo.UseShellExecute = false;
53  
54                 cmd.StartInfo.RedirectStandardInput = true;
55                 cmd.StartInfo.RedirectStandardOutput = true;
56  
57                 cmd.StartInfo.CreateNoWindow = true;
58                 cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
59  
60                 cmd.Start();
61  
62                 output = cmd.StandardOutput.ReadToEnd();
63                 Console.WriteLine(output);
64                 cmd.WaitForExit();
65                 cmd.Close();
66             }
67             catch (Exception e)
68             {
69                 Console.WriteLine(e);
70             }
71             return output;
72         }
73     }
74 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章