C# 執行/關閉 BAT檔案

C# 執行/關閉 BAT檔案

C# 執行/關閉
BAT檔案


 

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;

//http://fecbob.pixnet.net/blog/post/38088805-c%23-%E5%95%9F%E5%8B%95%E5%A4%96%E9%83%A8%E7%A8%8B%E5%BC%8F

/*

C# 啟ÓO動XE外D~部!程g式!的o幾¦X種O方e法k:G

1. 啟ÓO動XE外D~部!程g式!,A不¢G等g¢D待Y其La退Xh出DX。C

2. 啟ÓO動XE外D~部!程g式!,A等g¢D待Y其La退Xh出DX。C

3. 啟ÓO動XE外D~部!程g式!,A無gL限–等g¢D待Y其La退Xh出DX。C

4. 啟ÓO動XE外D~部!程g式!,A通q過L事LA件Do監E視go其La退Xh出DX。C

 

// using System.Diagnostics;

private string appName = “calc.exe”;

 

/// <summary>

/// 1. 啟ÓO動XE外D~部!程g式!,A不¢G等g¢D待Y其La退Xh出DX

/// </summary>

private void button1_Click(object sender, EventArgs e)

{

Process.Start(appName);

MessageBox.Show(String.Format(“外D~部!程g式! {0} 啟ÓO動XE完±1成¡L!I”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

 

/// <summary>

/// 2. 啟ÓO動XE外D~部!程g式!,A等g¢D待Y其La退Xh出DX

/// </summary>

private void button2_Click(object sender, EventArgs e)

{

try

{

Process proc = Process.Start(appName);

if (proc != null)

{

proc.WaitForExit(3000);

if (proc.HasExited)

MessageBox.Show(String.Format(“外D~部!程g式! {0} 已w經Mg退Xh出DX!I”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

else

{

// 如p果G外D~部!程g式!沒LS有3結g2束±o運B行a則h強Ój行a終¡Ñ止i之¡±。C

proc.Kill();

MessageBox.Show(String.Format(“外D~部!程g式! {0} 被Q強Ój行a終¡Ñ止i!I”,
this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

}

}

catch (ArgumentException ex)

{

MessageBox.Show(ex.Message, this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

 

/// <summary>

/// 3. 啟ÓO動XE外D~部!程g式!,A無gL限–等g¢D待Y其La退Xh出DX

/// </summary>

private void button3_Click(object sender, EventArgs e)

{

try

{

Process proc = Process.Start(appName);

if (proc != null)

{

proc.WaitForExit();

MessageBox.Show(String.Format(“外D~部!程g式! {0} 已w經Mg退Xh出DX!I”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

catch (ArgumentException ex)

{

MessageBox.Show(ex.Message, this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

 

/// <summary>

/// 4. 啟ÓO動XE外D~部!程g式!,A通q過L事LA件Do監E視go其La退Xh出DX

/// </summary>

private void button4_Click(object sender, EventArgs e)

{

try

{

// 啟ÓO動XE外D~部!程g式!

Process proc = Process.Start(appName);

if (proc != null)

{

// 監E視go進i程g退Xh出DX

proc.EnableRaisingEvents = true;

// 指u定w退Xh出DX事LA件Do方e法k

proc.Exited += new EventHandler(proc_Exited);

}

}

catch (ArgumentException ex)

{

MessageBox.Show(ex.Message, this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

/// <summary>

/// 啟ÓO動XE外D~部!程g式!退Xh出DX事LA件Do

/// </summary>

void proc_Exited(object sender, EventArgs e)

{

MessageBox.Show(String.Format(“外D~部!程g式! {0} 已w經Mg退Xh出DX!I”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

*/

namespace WinForm_Process_test

{

    public partial class Form1 : Form

    {

        private
string appName = “123.bat”;

        private
Process m_pro;

        public
int m_id;

        public
Form1()

        {

            InitializeComponent();

           

        }

 

        private
void button1_Click(object
sender, EventArgs e)

        {

            if
(m_pro == null)

            {

                /*

                ProcessStartInfo
startInfo = new ProcessStartInfo(“cmd.exe”,”/c
c:\\123.bat”);

               
startInfo.WindowStyle = ProcessWindowStyle.Normal;

                */

                m_pro = Process.Start(“c:\\123.bat”);

                m_id = m_pro.Id;

            }

        }

 

        private
void button2_Click(object
sender, EventArgs e)

        {

            if
(m_pro != null)

            {

                if
(!m_pro.HasExited)//
取得程式!是否已執行完成

                {

                    try

                    {

                        Process[] javaProcList = Process.GetProcessesByName(“ping”);//要先關閉BAT內的執行程式! 否則無法關閉BAT
2015/12/02

                        foreach (Process
javaProc in javaProcList)

                        {

                            javaProc.Kill();

                            javaProc.Close();

                           
javaProc.Dispose();

                        }

                        m_pro.Kill();

                        m_pro.Close();

                        m_pro.Dispose();

                        m_pro = null;

                    }

                    catch
(Exception exp)

                    {

                    }

                }

            }

        }

    }

}

 

_

 


 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *