rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » C#

C#运行外部程序

使用方法:
System.Diagnostics.Process.Start("iexplore.exe", "http://hi.baidu.com/marsbook");

说明:
参数1为要运行的外部程序,参数2为所要运行程序的参数。

  这里所举的例子是用IE浏览器打开一个网址,而且是不会在IE的同一窗口打开页面,也就是每次都会用一个新的窗口打开这个链接所指向的页面。这个也可以用于LinkLabel控件实现WEB链接效果。

using System.Diagnostics . .
private void buttonItem3_Click(object sender, System.EventArgs e)     Process mPro=new Process();
    string path=System.Environment.GetFolderPath(Environment.SpecialFolder.System);
    mPro.StartInfo.FileName=path+"";
    mPro.Start();
   }

C#程序调用外部程序

/*
*   编程语言:Visual   Studio   .NET   C#   (Beta   2) *   功         能:通过C#程序调用   Windows   记事本程序   编辑一个
*   名为   test.txt   的文本文件。 *   在整个程序中   System.Diagnostics.Process.Start(Info)   *   如果只是单独执行一个外部程序,可用一条如下代码即可:
*   System.Diagnostics.Process.Start( */ using   System; class   test static   void   Main() System.Diagnostics.ProcessStartInfo   Info   =   new   System.Diagnostics.ProcessStartInfo(); //设置外部程序名
Info.FileName   =   "notepad.exe"; //设置外部程序的启动参数(命令行参数)为test.txt
Info.Arguments   =   "test.txt"; //设置外部程序工作目录为   C:\
Info.WorkingDirectory   =   "C:\\"; //声明一个程序类
System.Diagnostics.Process   Proc   ; try // //
Proc   =   System.Diagnostics.Process.Start(Info); catch(System.ComponentModel.Win32Exception   e) Console.WriteLine("系统找不到指定的程序文件。\r{0}",   e);
return; Console.WriteLine("外部程序的开始执行时间:{0}",   Proc.StartTime); //等待3秒钟
Proc.WaitForExit(3000); //如果这个外部程序没有结束运行则对其强行终止
if(Proc.HasExited   ==   false) Console.WriteLine("由主程序强行终止外部程序的运行!");
Proc.Kill(); else Console.WriteLine("由外部程序正常退出!"); Console.WriteLine("外部程序的结束运行时间:{0}",   Proc.ExitTime);
Console.WriteLine("外部程序在结束运行时的返回值:{0}",   Proc.ExitCode); }

C#调用外部程序的问题。

悬赏分:15 - 解决时间:2008-8-18 15:18 比如有4个EXE文件分别是a.exe,b.exe,c.exe,d.exe。
项目中我要用到这4个外部程序。必须要调用。exe的代码我不知道(涉及到知识产权问题)。
于是乎我用了System.Diagnostics.Process编辑了4个方法依次调用了这4个EXE文件。但是问题来了,由于每个EXE程序所处理的文件都是上一个EXE文件生成的文件。就是说a.exe执行后生成个文件,b.exe要用到。b.exe生成的文件,c.exe要用到,c.exe生成的d.exe要用到。
但是往往是a.exe执行过程中文件还没有生成b.exe就开始处理了。所以经常弹出错误提示:找不到所需的文件。
后来我想到了用线程的方法处理,但是有个问题因为每次a.exe程序所调用的文件是用户可以任意选择的。数据集自然大小也不相同,所以程序执行的时间也不确定。而麻烦的是随着数据集的增大,我发现处理的时间并不是线性增长的。 说实话我不想用线程解决问题,因为我觉得线程很不稳定。而且不同环境运行速度也不一样。
我想问问方法有没有什么系统内置的返回值呢?能不能根据返回值判断下一个EXE文件什么时候开始执行呢?如果没有的话,怎么才能够解决掉这个问题呢?
请高手赐教!小P不胜感激! 提问者: - 试用期 一级 最佳答案 呵呵,刚给楼主写一个类,主要就是为了解决楼主这样需要依次执行多个程序的问题。 ExecuteHelper eh = new ExecuteHelper();
eh.AddExe("c:\....\a.exe"); eh.ExecuteFinished+=new ExecuteHelper.ExecuteFinishedEventHandler(...); //开始执行
eh.StartExecute();
-------
完整类代码如下: public class ExecuteHelper /// <summary> /// </summary>
private Queue<string> _executeableFiles { get; set; } /// <summary> /// </summary>
public bool IsInRun { get; set; } /// <summary> /// </summary>
/// <param name="path"></param>
public void AddExe(string path) if (IsInRun) { throw new InvalidOperationException("正在执行中,无法添加任务。"); return; } _executeableFiles.Enqueue(path);
} public ExecuteHelper() { _executeableFiles = new Queue<string>(); } /// <summary> /// </summary>
public void StartExecute() if (IsInRun) { throw new InvalidOperationException("正在执行中,无法重新任务。"); return; } IsInRun = true;
DoWork(null, null);
} #region 事件 /// <summary> /// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void ExecuteFinishedEventHandler(object sender, EventArgs e); /// <summary> /// </summary>
public event ExecuteFinishedEventHandler ExecuteFinished;
#endregion #region 内部函数 void DoWork(object sender, EventArgs e) if (_executeableFiles.Count == 0) if (ExecuteFinished != null) ExecuteFinished(this, new EventArgs());
IsInRun = false; else string fn = _executeableFiles.Dequeue(); System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(fn);
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(DoWork);
p.Start(psi); }
#endregion
}
顶一下
(0)
踩一下
(0)