C#调用cmd.exe程序和外部程序
来源:互联网 发布:知茵女装这个牌子 编辑:程序博客网 时间:2024/06/05 17:58
using System;
using System.Diagnostics;
namespace ApplyCmd
{
///
/// CmdUtility 的摘要说明。
///
public class CmdUtility
{
///
/// 执行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();
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;
}
}
}
ps:利用System.Diagnostics.Process来压缩文件或文件夹
string strArg = "a -r {0} {1}";
System.Diagnostics.Process.Start(@"C:Program FilesWinRAR ar.exe", String.Format(strArg, txtApp.Text+".rar", txtApp.Text));
strArg为winrar的命令参数,请参考帮助。
using System.Diagnostics;
namespace ApplyCmd
{
///
/// CmdUtility 的摘要说明。
///
public class CmdUtility
{
///
/// 执行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();
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;
}
}
}
ps:利用System.Diagnostics.Process来压缩文件或文件夹
string strArg = "a -r {0} {1}";
System.Diagnostics.Process.Start(@"C:Program FilesWinRAR ar.exe", String.Format(strArg, txtApp.Text+".rar", txtApp.Text));
strArg为winrar的命令参数,请参考帮助。
- C#调用cmd.exe程序和外部程序
- C#调用cmd.exe程序和外部程序
- C#调用cmd.exe程序和外部程序
- C#调用cmd.exe程序和外部程序
- 调用cmd.exe程序和外部程序
- 调用cmd.exe程序和外部程序(转)
- C#调用cmd.exe程序
- c#调用外部exe程序
- c#调用外部exe程序
- C#程序调用cmd.exe执行命令
- C#如何调用外部exe程序
- C#窗体内嵌外部程序(cmd.exe)的显示
- 使用java调用本地exe程序和cmd程序
- C#调用外部程序(用cmd为例子)
- C# 调用外部程序
- C#调用外部程序
- C#调用外部程序
- C#调用外部程序
- 日本政府称经济景气正在下滑
- 24岁之两个轮回完结篇
- PC Camera 开发日志(十六)------- INI 文件读写
- 朝鲜反美宣传海报在国际掀起高价收藏风(组图)
- C#操作注册表及设置开机自动运行
- C#调用cmd.exe程序和外部程序
- 财政部长有意调高个税起征点
- 大连散客旅游网今日开通一站式服务升级滨城自助游
- 经济放缓 全球角色逆转
- 全角半角转换函数
- 关于#include "stdafx.h"(转)
- 奥运赛程
- 一个VB.NET与C#代码在线互转的网站
- 网页下拉菜单制作完全攻略