怎样在C#代码中实现一个执行文件的执行(*.exe) 使用API函数shellexec(参数列表..) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接用Process类就行了。添加命名空间System.Diagnostics,代码如下:Process ps = new Process();ps.StartInfo.FileName = @"E:\WINDOWS\regedit.exe";ps.Start(); Process.Start("osql","/E /i c:\\restor1.sql"); 下面是我的一个类里面的其中两个方法,用来执行命令行程序./// <summary>/// 执行单条命令/// </summary>/// <param name="commandText">命令文本</param>/// <returns>命令输出文本</returns>public static string ExeCommand(string commandText){ 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(); p.StandardInput.WriteLine(commandText); p.StandardInput.WriteLine("exit"); //string formats = s.Replace("\r","").Replace("\n","\r\n"); strOutput = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); } catch(Exception e) { strOutput = e.Message; } return strOutput;}/// <summary>/// 执行多条命令/// </summary>/// <param name="commandArray">命令文本数组</param>/// <returns>命令输出文本</returns>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程序。/// <summary>/// 启动外部应用程序/// </summary>/// <param name="appName">应用程序路径名称</param>/// <returns>true表示成功,false表示失败</returns>public static bool StartApp(string appName){ bool blnRst = false; Process p = new Process(); p.StartInfo.FileName = appName;//exe,bat and so on p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; try { p.Start(); p.WaitForExit(); p.Close(); //while (!p.HasExited) //{ // Thread.Sleep(1000); //} blnRst = true; } catch { } return blnRst;} if (System.IO.File.Exists(Application.StartupPath+@"\update.exe")) { System.Diagnostics.Process.Start("update.exe"); Application.Exit(); } else { MessageBox.Show("升级文件不存在!"); } rhs(行云流水) ( ) 的方法不错 我觉得用process最好了用StartInfo.FileName接收要启动的.exe文件的名字用Start()函数启动!!! C#byte[]数据进行高低位转换 C# AxF1Book 怎么象标准控件拖拉显示到窗口上 .net 打印gridview 或者 table 非常感谢! 明明是用vs05开发的,之前还能打开,现在却提示无法打开 CrystalReportView只显示标题没有内容 在wpf中该怎么实现这个需求呢? 刚写的小程序,为什么不能显示密码修改正确?? 图片用datagrid怎么在网页中显示 求高手讲解下如何实现类似ctrl+f的查询下一条功能 C#如何启动autocad2014 windows服务里可以使用timer吗? 谁能帮忙解释一下API、COM、COM+这几个概念?
添加命名空间System.Diagnostics,
代码如下:
Process ps = new Process();
ps.StartInfo.FileName = @"E:\WINDOWS\regedit.exe";
ps.Start();
/// <summary>
/// 执行单条命令
/// </summary>
/// <param name="commandText">命令文本</param>
/// <returns>命令输出文本</returns>
public static string ExeCommand(string commandText)
{
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();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
//string formats = s.Replace("\r","").Replace("\n","\r\n");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
/// <summary>
/// 执行多条命令
/// </summary>
/// <param name="commandArray">命令文本数组</param>
/// <returns>命令输出文本</returns>
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程序。
/// <summary>
/// 启动外部应用程序
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName)
{
bool blnRst = false;
Process p = new Process();
p.StartInfo.FileName = appName;//exe,bat and so on
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
p.Start();
p.WaitForExit();
p.Close();
//while (!p.HasExited)
//{
// Thread.Sleep(1000);
//}
blnRst = true;
}
catch
{
}
return blnRst;
}
{
System.Diagnostics.Process.Start("update.exe");
Application.Exit();
}
else
{
MessageBox.Show("升级文件不存在!");
}
的方法不错
用StartInfo.FileName接收要启动的.exe文件的名字
用Start()函数启动!!!