往往会在程序A中调用应用程序B执行一个命令,如何调用B,以及判断B是否已经执行完毕,是一件很麻烦的事。下面代码可以很容易解决这个问题。

 

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace RegeditApplication
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//声明一个程序信息类
            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;
            }

            
//判断程序是否在运行
            bool isExist = false;
            
while (!isExist)
            {
                Proc.WaitForExit(
3000);
                
if (Proc.HasExited == false)
                {
                    Console.WriteLine(
"still running");
                }
                
else
                {
                    Console.WriteLine(
"is finished");
                    isExist 
= true;
                }
            }
            Console.Read();
        }

    }
}

 

转载于:https://www.cnblogs.com/thornfield_he/archive/2009/11/16/1603919.html

更多推荐