创建一个进程并调用(.net)

        最近有一个项目需求,需要调用一个exe,就上网查询了一下,顺利的完成了工作,感觉虽然简单,但挺有意思,就记录一下。

    一,创建一个进程

          1,代码视图(控制台程序)

              

          2,代码        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace KillProcess
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
                return;
            if (args[0].Equals("help") || args[0].Equals("?")||args[0].Equals("-help"))
            {
                Console.WriteLine(" 使用该进程,可以杀掉进程 命令形式如下:");
                Console.WriteLine("  KillProcess [-ProcessName]");
                Console.WriteLine("  ProcessName 要杀掉的进程的名称");
            }
            Process[] ps = null;
            foreach (String pName in args)
            {
                ps = Process.GetProcessesByName(pName);
                if (ps != null && ps.Length > 0)
                {
                    foreach (Process p in ps)
                    {
                        p.Kill();
                    }
                }
            }
        }
    }
}
View Code

    二,用CMD调用

          

    三,用程序调用

          1.代码视图

              

          2.代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace InvokeProcess
{
    class Program
    {
        static void Main(string[] args)
        {

            Process process = new Process();

            process.StartInfo.WorkingDirectory = @"E:\AA\ProcessTest\KillProcess\bin\Debug\";
            process.StartInfo.Arguments = "notepad";
            process.StartInfo.FileName = "KillProcess";

            process.Start();

            process.WaitForExit();


        }
    }
}
View Code

 

posted @ 2014-06-24 23:13  --中庸--  阅读(327)  评论(0编辑  收藏  举报