windows启动应用程序

 

 

在 Winform 里有时会需要打开另一个应用程序或文件,比如打开浏览器、打开word文档、打开文件夹和打印文件等等。本文介绍用 C# 在 Winform 中打开一个新进程,完成上述功能。

using System.Diagnostics

该命名空间提供与系统进程、事件日志、性能计数器的交互。其中与进程相关的两个基本类是 System.Diagnostics.Process 和 System.Diagnostics.ProcessStartInfo

System.Diagnostics.Procss:提供对本地和远程进程的访问,并使您能够启动和停止本地系统进程.
(1)  Start ():启动进程,主要有如下参数设置
Start( ProcessStartInfo )
Start( string FileName )
Start( string FileName, string Arguments )

(2) 属性:
Id:唯一进程标识号
ProcessName:进程名称
MachineName:进程运行所在的计算机名
StartInfo:进程的 StartInfo
StartTime:启动进程的时间
ExitTime:退出进程的时间
HasExited:进程是否已经终止

System.Diagnostics.ProcessStartInfo:与 Process 一起使用,为 Process 设置启动参数
(1)  构造方法:
ProcessStartInfo ()
ProcessStartInfo ( string FileName )
ProcessStartInfo ( string FileName, string Arguments)

(2)  一些属性:
FileName:应用程序或文件名
Arguments:参数
WorkingDirectory:启动进程的初始目录
CreateNoWindow:是否在新窗口启动进程
WindowStyle:指定打开窗口时的状态(枚举值)
Verb:打开进程时需要使用的谓词;每个文件扩展名都有它自己的一组谓词;可以使用 Verbs 属性获取这些谓词。例如,“print”谓词将打印使用 FileName 指定的文档。可使用空字符串 ("") 指定默认谓词。
 

下面用一个 WinForm 小程序来简单实现打开进程:
image

首先,引入命名空间: using System.Diagnostics;

 

(1) 打开文件

复制代码

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            // 定义一个 ProcessStartInfo 实例
              ProcessStartInfo psi = new ProcessStartInfo();
            // 设置启动进程的初始目录
              psi.WorkingDirectory = Application.StartupPath;
            // 设置启动进程的应用程序或文档名
              psi.FileName = @"xwy20110619.txt";
            // 设置启动进程的参数
              psi.Arguments = "";
            //启动由包含进程启动信息的进程资源
              try
            {
                Process.Start(psi);
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
复制代码
 

(2) 打开浏览器

复制代码

        private void btnOpenIE_Click(object sender, EventArgs e)
        {
            // 启动IE进程
              Process.Start("IExplore.exe");
        }
复制代码

 

(2) 打开指定 URL

复制代码

        private void btnOpenUrl_Click(object sender, EventArgs e)
        {
            // 方法一 
              // 启动带参数的IE进程
              Process.Start("IExplore.exe", "http://www.cnblogs.com/SkySoot/");

            // 方法二
              // 定义一个ProcessStartInfo实例
              ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            // 设置进程参数
              startInfo.Arguments = "http://www.cnblogs.com/SkySoot/";
            // 并且使进程界面最小化
              startInfo.WindowStyle = ProcessWindowStyle.Minimized;
            // 启动进程
              Process.Start(startInfo);
        }
复制代码

 

(2) 打开文件夹

复制代码

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            // 获取“收藏夹”文件路径
              string myFavoritesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
            // 启动进程
              System.Diagnostics.Process.Start(myFavoritesPath);
        }
复制代码

 

(2) 打印文档

复制代码

        private void btnPrintDoc_Click(object sender, EventArgs e)
        {
            // 定义一个进程实例
              Process myProcess = new Process();
            try
            {
                // 设置进程的参数
                  string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                myProcess.StartInfo.FileName = myDocumentsPath + "\\TxtForTest.txt";
                myProcess.StartInfo.Verb = "Print";

                // 显示txt文件的所有谓词: Open,Print,PrintTo
                foreach (string v in myProcess.StartInfo.Verbs)
                {
                    MessageBox.Show(v);
                }

                // 是否在新窗口中启动该进程的值
                  myProcess.StartInfo.CreateNoWindow = true;
                // 启动进程
                  myProcess.Start();
            }
            catch (Win32Exception ex)
            {
                if (ex.NativeErrorCode == 1)
                {
                    MessageBox.Show(ex.Message + " Check the path." + myProcess.StartInfo.FileName);
                }
                else if (ex.NativeErrorCode == 2)
                {
                    MessageBox.Show(ex.Message + " You do not have permission to print this file.");
                }
            }
复制代码

 

posted @ 2022-03-28 18:18  bingxingc  阅读(295)  评论(0编辑  收藏  举报