.NET 程序,调用其它程序
2024-07-10 13:05:31
供稿:网友
.net 程序,调用其它程序
using system;
using system.diagnostics;
namespace jmv_demo
{
/// <summary>
/// cmdutility 的摘要说明。
/// </summary>
public class cmdutility
{
/// <summary>
/// 执行cmd.exe命令
/// </summary>
/// <param name="commandtext">命令文本</param>
/// <returns>命令输出文本</returns>
public string execommand(string commandtext)
{
return execommand(new string []{commandtext});
}
/// <summary>
/// 执行多条cmd.exe命令
/// </summary>
/// <param name="commandarray">命令文本数组</param>
/// <returns>命令输出文本</returns>
public 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 bool startapp(string appname)
{
return startapp(appname,processwindowstyle.hidden);
}
/// <summary>
/// 启动外部应用程序
/// </summary>
/// <param name="appname">应用程序路径名称</param>
/// <param name="style">进程窗口模式</param>
/// <returns>true表示成功,false表示失败</returns>
public 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 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 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;
}
}
}