一个执行Dos命令的窗口程序,与各位分享。
效果图:
具体实现在代码中有详细的注释,请看代码。
实现执行CMD命令的核心代码(Cmd.cs):
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Management;
using System.Globalization;
namespace Zbsoft.ExtendFunction
{
public class Cmd
{
/// <summary>
/// 是否终止调用CMD命令执行
/// </summary>
private static bool invokeCmdKilled = true;
/// <summary>
/// 获取或设置是否终止调用CMD命令执行
/// </summary>
public static bool InvokeCmdKilled
{
get { return invokeCmdKilled; }
set
{
invokeCmdKilled = value;
if (invokeCmdKilled)
{
if (p != null && !p.HasExited)
{
killProcess(p.Id);
}
}
}
}
private static Process p;
private static Action<string> RefreshResult;
/// <summary>
/// 调用CMD命令,执行指定的命令,并返回命令执行返回结果字符串
/// </summary>
/// <param name="cmdArgs">命令行</param>
/// <param name="RefreshResult">刷新返回结果字符串的事件</param>
/// <returns></returns>
public static void InvokeCmd(string cmdArgs, Action<string> pRefreshResult = null)
{
InvokeCmdKilled = false;
RefreshResult = pRefreshResult;
if (p != null)
{
p.Close();
p = null;
}
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
string[] cmds = cmdArgs.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var v in cmds)
{
Thread.Sleep(200);
p.StandardInput.WriteLine(v);
}
//p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Dispose();
p.Close();
p = null;
InvokeCmdKilled = true;
}
/// <summary>
/// 输入交互式命令
/// </summary>
/// <param name="cmd"></param>
public static void InputCmdLine(string cmd)
{
if (p == null) return;
string[] cmds = cmd.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var v in cmds)
{
Thread.Sleep(200);
p.StandardInput.WriteLine(v);
}
}
/// <summary>
/// 异步读取标准输出信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>