现在的位置: 首页 > 综合 > 正文

C#调用Exe 及 有参数的

2012年05月01日 ⁄ 综合 ⁄ 共 5697字 ⁄ 字号 评论关闭

在编写程序时经常会使用到调用可执行程序的情况,本文将简单介绍C#调用exe的方法。在C#中,通过Process类来进行进程操作。 Process类在System.Diagnostics包中。

示例一

using System.Diagnostics;

Process p = Process.Start("notepad.exe");
p.WaitForExit();//关键,等待外部程序退出后才能往下执行

通过上述代码可以调用记事本程序,注意如果不是调用系统程序,则需要输入全路径。

示例二

当需要调用cmd程序时,使用上述调用方法会弹出令人讨厌的黑窗。如果要消除,则需要进行更详细的设置。

Process类的StartInfo属性包含了一些进程启动信息,其中比较重要的几个

FileName                可执行程序文件名

Arguments              程序参数,已字符串形式输入
CreateNoWindow     是否不需要创建窗口
UseShellExecute      是否需要系统shell调用程序

通过上述几个参数可以让讨厌的黑屏消失

System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = binStr;
exep.StartInfo.Arguments = cmdStr;
exep.StartInfo.CreateNoWindow = true;
exep.StartInfo.UseShellExecute = false;
exep.Start();
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行

 

或者

System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = binStr;
startInfo.Arguments = cmdStr;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.Start(startInfo);
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行

 

using   System.Diagnostics;  
   如果是dos  
   Process.Start("cmd.exe");  
   如果是其他文件  
   Process.Start("绝对路径+文件名.exe");  
   ------------------------------------  
   如何在c#中调用外部dos程序?  
   使用Process对象:      
   System.Diagnostics.Process     p=new     System.Diagnostics.Process();      
   p.StartInfo.FileName="arj.exe"     ;//需要启动的程序名      
   p.StartInfo.Arguments="-x     sourceFile.Arj     c:/temp";//启动参数      
   p.Start();//启动      
   if(p.HasExisted)//判断是否运行结束      
     p.kill();  
-------------------------------------------------------------------------------------------------------------------------------------
///   <summary>  
   ///   启动其他的应用程序  
   ///   </summary>  
   ///   <param   name="file">应用程序名称</param>  
   ///   <param   name="workdirectory">应用程序工作目录</param>  
   ///   <param   name="args">命令行参数</param>  
   ///   <param   name="style">窗口风格</param>  
   public   static   bool   StartProcess(string   file,string   workdirectory,string   args,ProcessWindowStyle   style)  
   {  
   try  
   {  
   Process   myprocess   =   new   Process();  
   ProcessStartInfo   startInfo   =   new   ProcessStartInfo(file,args);  
   startInfo.WindowStyle   =   style;  
   startInfo.WorkingDirectory   =   workdirectory;  
   myprocess.StartInfo   =   startInfo;  
   myprocess.StartInfo.UseShellExecute   =   false;  
   myprocess.Start();  
   return   true;  
   }  
   catch(Exception   e0)  
   {  
   MessageBox.Show("启动应用程序时出错!原因:"   +   e0.Message);  
   }  
   return   false;  
   }  
   
   
   
   string   parms   =   ""   +   GlobalObject.GetInstance().UserID   +   "   "   +   GlobalObject.GetInstance().UserPassword;  
   if   (PublicMethods.StartProcess(Application.StartupPath   +   @"/uptool/uptool.exe",Application.StartupPath   +   "//UpTool",parms,ProcessWindowStyle.Normal))  
   {  
   Environment.Exit(0);  
   }  
----------------------------------------------------------------------------------------------------------------------
Process.Start("IExplore.exe",   "http://community.csdn.net/Expert/topic/4021/4021327.xml?temp=.9493524");
System.Diagnostics.ProcessStartInfo   startInfo   =   new   System.Diagnostics.ProcessStartInfo(   );  
   startInfo.FileName   =   "执行EXE的文件名";  
   startInfo.Arguments   =   "参数数组";  
   System.Diagnostics.Process.Start(   startInfo   );
----------------------------------------------------------------------------------------------------------------------------
1.有好多时,我们需要调用外部的EXE程序,并且要等它运行完毕,我们才可以继续下面的动作,那我们怎样去实现了,请看以下代码.
         '怎样等待外部程序运行完毕.
         '从系统资料夹读入文件
         Dim sysFolder As String = _
                     Environment.GetFoldERPath(Environment.SpecialFolder.System)
         '创建一个新的进程结构
         Dim pInfo As New ProcessStartInfo()
         '设置其成员FileName为系统资料的Eula.txt
         pInfo.FileName = sysFolder & "/eula.txt"
         '运行该文件
         Dim p As Process = Process.Start(pInfo)
         '等待程序装载完成
         p.WaitForInputIdle()
         '等待进行程退出
         p.WaitForExit()
         '继续执行下面的代码
         MessageBox.Show("继续执行代码")

2.我们想在5秒钟后,强行关闭它.而不是需要我手工关闭.
     '设置退出时间
     Dim timeOut As Integer = 5000
     Dim sysFolder As String = _
         Environment.GetFolderPath(Environment.SpecialFolder.System)
     Dim pInfo As New ProcessStartInfo()
     pInfo.FileName = sysFolder & "/eula.txt"
     Dim p As Process = Process.Start(pInfo)
     p.WaitForInputIdle()
     p.WaitForExit(timeOut)
     '检查是否在超时前已关闭了.
     If p.HasExited = False Then
         '进行程还在运行
         '看进程有没有回应
         If p.Responding Then
             p.CloseMainWindow() '关闭窗口
         Else
             p.Kill()   '强行中断
         End If
     End If
     MessageBox.Show("继续执行代码")

 

在编写程序时经常会使用到调用可执行程序的情况,本文将简单介绍C#调用exe的方法。在C#中,通过Process类来进行进程操作。 Process类在System.Diagnostics包中。

示例一

using System.Diagnostics;

Process p = Process.Start("notepad.exe");
p.WaitForExit();//关键,等待外部程序退出后才能往下执行

通过上述代码可以调用记事本程序,注意如果不是调用系统程序,则需要输入全路径。

示例二

当需要调用cmd程序时,使用上述调用方法会弹出令人讨厌的黑窗。如果要消除,则需要进行更详细的设置。

Process类的StartInfo属性包含了一些进程启动信息,其中比较重要的几个

FileName                可执行程序文件名

Arguments              程序参数,已字符串形式输入
CreateNoWindow     是否不需要创建窗口
UseShellExecute      是否需要系统shell调用程序

通过上述几个参数可以让讨厌的黑屏消失

System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = binStr;
exep.StartInfo.Arguments = cmdStr;
exep.StartInfo.CreateNoWindow = true;
exep.StartInfo.UseShellExecute = false;
exep.Start();
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行

 

或者

System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = binStr;
startInfo.Arguments = cmdStr;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.Start(startInfo);
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行

抱歉!评论已关闭.