C#运行cmd命令

using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.Threading;
06using System.Diagnostics;
07 
08namespace 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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值