怎样在C#代码中实现一个执行文件的执行(*.exe)

sjhcsharp 2004-04-08 11:58:34
或如果我用StreamWriter 对象写的一个比如*.bat文件怎么 使它执行?
...全文
75 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fireocean 2004-04-13
  • 打赏
  • 举报
回复
rhs(行云流水) ( )
的方法不错
李洪喜 2004-04-13
  • 打赏
  • 举报
回复
if (System.IO.File.Exists(Application.StartupPath+@"\update.exe"))
{
System.Diagnostics.Process.Start("update.exe");
Application.Exit();
}
else
{
MessageBox.Show("升级文件不存在!");
}
zhaoxiaolin 2004-04-13
  • 打赏
  • 举报
回复
支持楼上的
sjhcsharp 2004-04-13
  • 打赏
  • 举报
回复
thanks all!
sexfreebird 2004-04-13
  • 打赏
  • 举报
回复
我觉得用process最好了
用StartInfo.FileName接收要启动的.exe文件的名字
用Start()函数启动!!!
mxmsoft 2004-04-13
  • 打赏
  • 举报
回复
楼上的写的很好
marvelstack 2004-04-10
  • 打赏
  • 举报
回复
下面是我的一个类里面的其中两个方法,用来执行命令行程序.
/// <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;
}
jimh 2004-04-09
  • 打赏
  • 举报
回复
使用API函数shellexec(参数列表..)
Samen168 2004-04-09
  • 打赏
  • 举报
回复
Process 支持!
shuliangqing 2004-04-09
  • 打赏
  • 举报
回复
Process.Start("osql","/E /i c:\\restor1.sql");
rhs 2004-04-09
  • 打赏
  • 举报
回复
直接用Process类就行了。
添加命名空间System.Diagnostics,
代码如下:
Process ps = new Process();
ps.StartInfo.FileName = @"E:\WINDOWS\regedit.exe";
ps.Start();

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧