在不同项目开的发过程中,可能会用到许多相同或类似的功能开发或业务处理,从抽象设计的角度考虑,通常会把这些功能或业务操作进行封装,方便在不同项目中调用,这也是重用思想的重要体现。本次分享的是在C#中实现cmd命令行操作的功能封装,包括执行指定的cmd命令,启用第三方进程,隐藏cmd窗口以后台进程方式运行,判断某个进程是否在运行,结束指定的进程等。
WinCmdHelper
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace Wongoing.Basic
{
/// <summary>
/// windows命令辅助类
/// </summary>
public class WinCmdHelper
{
#region 执行Dos命令
/// <summary>
/// 执行Dos命令
/// </summary>
/// <param name="cmd">Dos命令及参数</param>
/// <param name="isShowCmdWindow">是否显示cmd窗口</param>
/// <param name="isCloseCmdProcess">执行完毕后是否关闭cmd进程</param>
/// <returns>成功返回true,失败返回false</returns>
public static bool ExcuteDosCommand(string cmd, bool isShowCmdWindow, bool isCloseCmdProcess)
{
try
{
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = !isShowCmdWindow;
p.OutputDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs e) {
if (!String.IsNullOrEmpty(e.Data))
{
ICSharpCode.Core.LoggingService<WinCmdHelper>.Debug(e.Data);
}
});
p.Start();
StreamWriter cmdWriter = p.StandardInput;
p.BeginOutputReadLine();
if (!String.IsNullOrEmpty(cmd))
{
cmdWriter.WriteLine(cmd);
}
cmdWriter.Close();
p.WaitForExit();
if (isCloseCmdProcess)
{
p.Close();
}
ICSharpCode.Core.LoggingService<WinCmdHelper>.Info(String.Format("成功执行Dos命令[{0}]!", cmd));
return true;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<WinCmdHelper>.Error("执行命令失败,请检查输入的命令是否正确:" + ex.Message, ex);
return false;
}
}
#endregion
#region 判断指定的进程是否在运行中
/// <summary>
/// 判断指定的进程是否在运行中
/// </summary>
/// <param name="processName">要判断的进程名称,不包括扩展名exe</param>
/// <param name="processFileName">进程文件的完整路径</param>
/// <returns>存在返回true,否则返回false</returns>
public static bool CheckProcessExists(string processName, string processFileName)
{
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process p in processes)
{
if (!String.IsNullOrEmpty(processFileName))
{
if (processFileName == p.MainModule.FileName)
{
return true;
}
}
else
{
return true;
}
}
return false;
}
#endregion
#region 结束指定的windows进程
/// <summary>
/// 结束指定的windows进程,如果进程存在
/// </summary>
/// <param name="processName">进程名称,不包含扩展名</param>
/// <param name="processFileName">进程文件完整路径,如果为空则删除所有进程名为processName参数值的进程</param>
public static bool KillProcessExists(string processName, string processFileName)
{
try
{
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process p in processes)
{
if (!String.IsNullOrEmpty(processFileName))
{
if (processFileName == p.MainModule.FileName)
{
p.Kill();
p.Close();
}
}
else
{
p.Kill();
p.Close();
}
}
ICSharpCode.Core.LoggingService<WinCmdHelper>.Info(String.Format("成功结束[{0}]进程!", processes));
return true;
}
catch(Exception ex)
{
ICSharpCode.Core.LoggingService<WinCmdHelper>.Error("结束指定的Widnows进程异常:" + ex.Message, ex);
return false;
}
}
#endregion
}
}
1、因为我在封装的时候依赖了ICSharpCode.Core,如果您没有ICSharpCode,请把上面代码中使用
ICSharpCode.Core.LoggingService<WinCmdHelper>.Error
的地方改为
Console.WriteLine