網頁

2011年9月28日 星期三

[C#] 利用 Process 執行外部程式


[C#]利用 Process 來執行其它外部程式



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics; //for notepad.exe

namespace Instructions
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //開啟記事本
            Process notePad = new Process();
            // FileName 是要執行的檔案
            notePad.StartInfo.FileName = "notepad.exe";
            notePad.Start();

            //控制(關閉)外部程式
            // 先取得要控制的處理程序名稱
            Process[] processes = Process.GetProcessesByName("NotePad");
            foreach (Process p in processes)
            {
                // 關閉目前程序前先等待 1000 毫秒
                p.WaitForExit(1000);
                p.CloseMainWindow();
            }


        }
    }
}

C#利用Process關閉所有的IE窗口

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。