C# 利用PROCESS 開啟外部程式並且利用API控制縮小,最後再將開啟程式關閉(範例控制對象USBWebserver)
C# 利用PROCESS
開啟外部程式並且利用API控制縮小,最後再將開啟程式關閉(範例控制對象USBWebserver)
資料來源: 01.http://stackoverflow.com/questions/18652162/how-to-minimize-maximize-opened-applications
02.http://www.pinvoke.net/index.aspx(再.NET使用WINDOWS API教學網站)
03. http://www.usbwebserver.com/(USBWebserver官方網站)
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; using System.Runtime.InteropServices; //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”, 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 private //http://www.pinvoke.net/default.aspx/user32.showwindow [DllImport(“user32.dll”, EntryPoint = “FindWindow”)] public [DllImport(“user32.dll”)] [return: static private public public { InitializeComponent(); } private { if { /* ProcessStartInfo */ m_pro = Process.Start(@”D:\USBWebserver m_id = m_pro.Id; } } private { if { if { try { Process[] javaProcList = Process.GetProcessesByName(“usbwebserver”);//要n先Dy關o閉?BAT內o的o執Xo行a程g式! 否±_則h無gL法k關o閉?BAT foreach (Process { javaProc.Kill(); javaProc.Close(); } m_pro.Close(); m_pro.Dispose(); m_pro = null; } catch { } } } } private { IntPtr ShowWindow(hwnd, SW_MINIMIZE); } } } _ |