• System.Diagnostics.Process 测试案例


    1.System.Diagnostics.Process 执行exe文件

    创建项目,编译成功后,然后把要运行的exe文件拷贝到该项目的运行工作目录下即可,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    using System.Threading;
    
    namespace MyProcess
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process myProcess = new Process();
                //获取当前工作目录
                string strWorkDir = System.AppDomain.CurrentDomain.BaseDirectory;
                //获取或设置一个值,该值指示是否使用操作系统 shell 启动进程。如果应在启动进程时使用 shell,
                //则为 true;如果直接从可执行文件创建进程,则为 false。 默认值为 true
                myProcess.StartInfo.UseShellExecute = true;
                //获取或设置启动进程时使用的窗口状态
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                //获取或设置要启动的应用程序或文档
                myProcess.StartInfo.FileName = strWorkDir + "DataBinding.exe";
                //启动StartInfo属性指定的进程资源
                myProcess.Start();
                //本地计算机上运行的所有进程资源
                Process[] myCurrProcess = Process.GetProcesses();
                //遍历所有进程资源,找到关联进程
                foreach (Process process in myCurrProcess)
                {
                    if (0 == process.ProcessName.CompareTo("DataBinding"))
                    {
                        Console.WriteLine("DataBinding.exe正在运行中");
                    }
                }
                Thread.Sleep(1000);
                //关闭关联的进程
                myProcess.Kill();              
            }
        }
    }

    2.System.Diagnostics.Process运行浏览器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    using System.Threading;
    
    namespace MyProcess
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process.Start("http://www.baidu.com");
                Process myProcess = new Process();
                myProcess.StartInfo.FileName = "IEXPLORE.EXE";
                myProcess.StartInfo.Arguments = "http://www.baidu.com";
                myProcess.Start();
    
                Thread.Sleep(1000);
                myProcess.Kill();
            }
        }
    }

    3.System.Diagnostics.Process打开Word文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    using System.Threading;
    
    namespace MyProcess
    {
        class Program
        {
            static void Main(string[] args)
            {
                string fileName = "D:\201901151749.docx";
                string winwordPath = "";
                //判断系统中是否已经有Word实例在运行
                Process[] wordProcesses = Process.GetProcessesByName("winword");
                foreach(Process process in wordProcesses)
                {          
                    //如果有的话获得Winword.exe的完全限定名称
                    winwordPath = process.MainModule.FileName;
                    break;
                }
                Process wordProcess = new Process();
                if(winwordPath.Length > 0)//如果有Word实例在运行,使用"/w"参数来强制启动新实例,并将文件名作为参数传递
                {
                    wordProcess.StartInfo.FileName = winwordPath;
                    wordProcess.StartInfo.UseShellExecute = false;
                    wordProcess.StartInfo.Arguments = fileName + " /w";
                }
                else //如果没有Word实例运行,则打开fileNmae
                {
                    wordProcess.StartInfo.FileName = fileName;
                    wordProcess.StartInfo.UseShellExecute = true;
                }
    
                wordProcess.Start();
                wordProcess.WaitForExit();//当前进程一直在等待,直到Word实例退出
                wordProcess.Close();
            }
        }
    }
  • 相关阅读:
    luogu P1075 质因数分解
    luogu P1146 硬币翻转
    [HEOI2013]SAO
    [HAOI2010]软件安装
    [CodeForces-763C]Timofey and remoduling
    [CodeForces-375E]Red and Black Tree
    [CodeForces-178F]Representative Sampling
    [CodeForces-55D]Beautiful Numbers
    [AHOI2009]同类分布
    [ZJOI2010]数字计数
  • 原文地址:https://www.cnblogs.com/QingYiShouJiuRen/p/10273436.html
Copyright © 2020-2023  润新知