执行命令类
来源:互联网 发布:计算机编程是干什么的 编辑:程序博客网 时间:2024/04/30 00:53
最近经常要在C#中使用cmd命令,因此就封装了几个方法来使用。代码如下:
///
/// 执行cmd.exe命令
///
///命令文本
/// 命令输出文本
public static string ExeCommand(string commandText)
{
return ExeCommand(new string[] { commandText });
}
///
/// 执行多条cmd.exe命令
///
///命令文本数组
/// 命令输出文本
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();
//strOutput = Encoding.UTF8.GetString(Encoding.Default.GetBytes(strOutput));
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
///
/// 启动外部Windows应用程序,隐藏程序界面
///
///应用程序路径名称
/// true表示成功,false表示失败
public static bool StartApp(string appName)
{
return StartApp(appName, ProcessWindowStyle.Hidden);
}
///
/// 启动外部应用程序
///
///应用程序路径名称
///进程窗口模式
/// true表示成功,false表示失败
public static bool StartApp(string appName, ProcessWindowStyle style)
{
return StartApp(appName, null, style);
}
///
/// 启动外部应用程序,隐藏程序界面
///
///应用程序路径名称
///启动参数
/// true表示成功,false表示失败
public static bool StartApp(string appName, string arguments)
{
return StartApp(appName, arguments, ProcessWindowStyle.Hidden);
}
///
/// 启动外部应用程序
///
///应用程序路径名称
///启动参数
///进程窗口模式
/// true表示成功,false表示失败
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;
}
- 执行命令类
- 执行shell命令工具类
- 执行命令
- 执行命令
- 【执行】robot执行命令
- 一个执行CMD命令的工具类
- 一个执行CMD命令的工具类
- java方法执行控制台命令工具类
- Android 执行Linux命令工具类
- JAVA命令如何执行带包名的类
- 调用控制台执行命令工具类
- java执行cmd命令的分装类
- android 执行命令的shell工具类
- python 执行shell命令的类
- 执行linux命令,cmd命令
- jsp执行外部命令
- jsp执行外部命令
- UNIX后台执行命令
- QT 播放背景音乐
- 查看linux被隐藏进程的内存占用方法,可用于监控进程是否内存泄漏
- spring2.5.4+hibernate3.2.6+struts2+jbpm3.2.2
- 《身为女人》—写作者网作者:暖儿
- 用Intent调用系统中经常被用到的组件
- 执行命令类
- 《无废话XML》的一点笔记
- 珍惜每一天,快乐地过好每一天(很有道理)
- oracle 递归获取id和id的所有子机构
- Pocket PC、Pocket PC Phone、Smartphone的区别
- 三十五个非主流开源数据库 MongoDB领衔主演
- 代码性能分析-Dottrace跟踪代码执行时间
- NoSQL数据库—MongoDB入门使用和c#客户端的访问
- COM载板设计之三:I2C总线及其他信号