社区
C#
帖子详情
怎样在C#代码中实现一个执行文件的执行(*.exe)
sjhcsharp
2004-04-08 11:58:34
或如果我用StreamWriter 对象写的一个比如*.bat文件怎么 使它执行?
...全文
82
11
打赏
收藏
怎样在C#代码中实现一个执行文件的执行(*.exe)
或如果我用StreamWriter 对象写的一个比如*.bat文件怎么 使它执行?
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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();
第一章计算机系统概述.ppt
第一章计算机系统概述.ppt
智慧城市科技有限公司出资协议(确定稿).doc
智慧城市科技有限公司出资协议(确定稿).doc
智能化技术在电气工程自动化控制
中
的应用分析-1.docx
智能化技术在电气工程自动化控制
中
的应用分析-1.docx
网络玄幻小说受众特征研究.docx
网络玄幻小说受众特征研究.docx
基于CesiumJS的三维WebGIS研究与开发.docx
基于CesiumJS的三维WebGIS研究与开发.docx
C#
111,094
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章