C#调用外部应用程序

来源:互联网 发布:painter for mac 破解 编辑:程序博客网 时间:2024/04/30 08:42
using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;namespace WindowsApplication1{    class CallOtherApp    {        public static void CallAnotherApplication()        {            //声明一个程序信息类,该类存在于System.Diagnostics命名空间之下           ProcessStartInfo Info = new ProcessStartInfo();            //设置外部程序名            //Info.FileName = "notepad.exe";            Info.FileName = "thunder.exe";            //设置外部程序的启动参数(命令行参数)为test.txt            Info.Arguments = "thunder.exe";            //设置外部程序工作目录为 C:/            //Info.WorkingDirectory = "C://";            Info.WorkingDirectory = @"C:/Program Files/Thunder Network/Thunder";            //声明一个程序类            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);        }        /// <summary>        /// 调用外部应用程序        /// </summary>        /// <param name="AppPath">外部程序所在路径</param>        /// <param name="AppName">应用程序名称</param>        public static void CallAnotherApplication(string AppPath,string AppName)        {            //声明一个程序信息类,该类存在于System.Diagnostics命名空间之下            ProcessStartInfo Info = new ProcessStartInfo();            //设置外部程序名                       Info.FileName = AppName;            //设置外部程序的启动参数(命令行参数)为迅雷应用程序名           // Info.Arguments = "thunder.exe";            //设置外部程序工作目录为  
            Info.WorkingDirectory = AppPath;            //声明一个程序类            System.Diagnostics.Process Proc;            try            {                //                //启动外部程序                //                Proc = System.Diagnostics.Process.Start(Info);            }            catch (System.ComponentModel.Win32Exception e)            {                //Console.WriteLine("系统找不到指定的程序文件。/r{0}", e);                throw new Exception(e.Message);                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);        }    }}
原创粉丝点击