/// <summary>
/// CmdUtility 的摘要说明。
/// </summary>
public class CmdUtility
{
/// <summary>
/// 执行cmd.exe命令
/// </summary>
/// <param name="commandText">命令文本</param>
/// <returns>命令输出文本</returns>
public static string ExeCommand(string commandText)
{
return ExeCommand(new string[] { commandText });
}
/// <summary>
/// 执行多条cmd.exe命令
/// </summary>
/// <param name="commandArray">命令文本数组</param>
/// <returns>命令输出文本</returns>
public static string ExeCommand(string[] commandTexts)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
foreach (string item in commandTexts)
{
p.StandardInput.WriteLine(item);
}
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
/// <summary>
/// 启动外部Windows应用程序,隐藏程序界面
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName)
{
return StartApp(appName, ProcessWindowStyle.Hidden);
}
/// <summary>
/// 启动外部应用程序
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <param name="style">进程窗口模式</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName, ProcessWindowStyle style)
{
return StartApp(appName, null, style);
}
/// <summary>
/// 启动外部应用程序,隐藏程序界面
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <param name="arguments">启动参数</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName, string arguments)
{
return StartApp(appName, arguments, ProcessWindowStyle.Hidden);
}
/// <summary>
/// 启动外部应用程序
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <param name="arguments">启动参数</param>
/// <param name="style">进程窗口模式</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName, string arguments, ProcessWindowStyle style)
{
bool blnRst = false;
Process p = new Process();
p.StartInfo.FileName = appName;//exe,bat and so on
p.StartInfo.WindowStyle = style;
p.StartInfo.Arguments = arguments;
try
{
p.Start();
p.WaitForExit();
p.Close();
blnRst = true;
}
catch
{
}
return blnRst;
}
}
下面是一个完整的程序,当然对Ping.exe程序执行的结果不全,读者可以进一步修改
完整代码如下:
1
using System;
2
using System.Diagnostics;
3
namespace ZZ
4
{
5
class ZZConsole
6
{
7
[STAThread]
8
static void Main(string[] args)
9
{
10
string ip = "192.192.132.229";
11
string strRst = CmdPing(ip);
12
Console.WriteLine(strRst);
13
Console.ReadLine();
14
}
15
private static string CmdPing(string strIp)
16
{
17
Process p = new Process();
18
p.StartInfo.FileName = "cmd.exe";
19
p.StartInfo.UseShellExecute = false;
20
p.StartInfo.RedirectStandardInput = true;
21
p.StartInfo.RedirectStandardOutput = true;
22
p.StartInfo.RedirectStandardError = true;
23
p.StartInfo.CreateNoWindow = true;
24
string pingrst;
25
p.Start();
26
p.StandardInput.WriteLine("ping -n 1 "+strIp);
27
p.StandardInput.WriteLine("exit");
28
string strRst = p.StandardOutput.ReadToEnd();
29
if(strRst.IndexOf("(0% loss)")!=-1)
30
pingrst = "连接";
31
else if( strRst.IndexOf("Destination host unreachable.")!=-1)
32
pingrst = "无法到达目的主机";
33
else if(strRst.IndexOf("Request timed out.")!=-1)
34
pingrst = "超时";
35
else if(strRst.IndexOf("Unknown host")!=-1)
36
pingrst = "无法解析主机";
37
else
38
pingrst = strRst;
39
p.Close();
40
return pingrst;
41
}
42
}
43
}
using System;2
using System.Diagnostics;3
namespace ZZ4
{5
class ZZConsole6
{7
[STAThread]8
static void Main(string[] args)9
{ 10
string ip = "192.192.132.229";11
string strRst = CmdPing(ip);12
Console.WriteLine(strRst);13
Console.ReadLine();14
}15
private static string CmdPing(string strIp)16
{17
Process p = new Process();18
p.StartInfo.FileName = "cmd.exe";19
p.StartInfo.UseShellExecute = false;20
p.StartInfo.RedirectStandardInput = true;21
p.StartInfo.RedirectStandardOutput = true;22
p.StartInfo.RedirectStandardError = true;23
p.StartInfo.CreateNoWindow = true;24
string pingrst;25
p.Start();26
p.StandardInput.WriteLine("ping -n 1 "+strIp);27
p.StandardInput.WriteLine("exit");28
string strRst = p.StandardOutput.ReadToEnd();29
if(strRst.IndexOf("(0% loss)")!=-1)30
pingrst = "连接";31
else if( strRst.IndexOf("Destination host unreachable.")!=-1)32
pingrst = "无法到达目的主机";33
else if(strRst.IndexOf("Request timed out.")!=-1)34
pingrst = "超时";35
else if(strRst.IndexOf("Unknown host")!=-1)36
pingrst = "无法解析主机";37
else38
pingrst = strRst;39
p.Close();40
return pingrst;41
}42
}43
}总结,这里就是为了说明一个问题,不但是Ping命令,只要是命令行程序或者是Dos内部命令,我们都可以用上面的方式来执行它,并获取相应的结果,并且这些程序的执行过程不会显示出来,如果需要调用外部程序就可以嵌入到其中使用了。
如果运行一个命令程序
System.Diagnostics.Process bakupProcess = new System.Diagnostics.Process();
bakupProcess.StartInfo.FileName = fileName;
bakupProcess.StartInfo.Arguments = arguments;
bakupProcess.Start();
bakupProcess.StartInfo.FileName = fileName;
bakupProcess.StartInfo.Arguments = arguments;
bakupProcess.Start();
private void button1_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start("cmd.exe",@" /c "+tbxCmd.Text.Trim()+@" c: > a.txt");
}
private void button2_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe",@"a.txt");
}




浙公网安备 33010602011771号