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# ÓOXED~!g!o¦XOekG

1. ÓOXED~!g!A¢Gg¢DYLa退XhDXC

2. ÓOXED~!g!Ag¢DYLa退XhDXC

3. ÓOXED~!g!AgLg¢DYLa退XhDXC

4. ÓOXED~!g!AqLLADoEgoLa退XhDXC

 

// using System.Diagnostics;

private string appName = “calc.exe”;

 

/// <summary>

/// 1. ÓOXED~!g!A¢Gg¢DYLa退XhDX

/// </summary>

private void button1_Click(object sender, EventArgs e)

{

Process.Start(appName);

MessageBox.Show(String.Format(“D~!g! {0} ÓOXE±1¡LI”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

 

/// <summary>

/// 2. ÓOXED~!g!Ag¢DYLa退XhDX

/// </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} wMg退XhDXI”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

else

{

// pGD~!g!LS3g2±oBahÓja¡Ñi¡±C

proc.Kill();

MessageBox.Show(String.Format(“D~!g! {0} QÓja¡ÑiI”,
this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

}

}

catch (ArgumentException ex)

{

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

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

 

/// <summary>

/// 3. ÓOXED~!g!AgLg¢DYLa退XhDX

/// </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} wMg退XhDXI”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

catch (ArgumentException ex)

{

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

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

 

/// <summary>

/// 4. ÓOXED~!g!AqLLADoEgoLa退XhDX

/// </summary>

private void button4_Click(object sender, EventArgs e)

{

try

{

// ÓOXED~!g!

Process proc = Process.Start(appName);

if (proc != null)

{

// Egoig退XhDX

proc.EnableRaisingEvents = true;

// uw退XhDXLADoek

proc.Exited += new EventHandler(proc_Exited);

}

}

catch (ArgumentException ex)

{

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

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

/// <summary>

/// ÓOXED~!g!退XhDXLADo

/// </summary>

void proc_Exited(object sender, EventArgs e)

{

MessageBox.Show(String.Format(“D~!g! {0} wMg退XhDXI”, 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)

                    {

                    }

                }

            }

        }

    }

}

 

_

 


 


發表迴響

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