簡單掌握Windows中C#啟動(dòng)外部程序進(jìn)程的方法
許多用戶在程序開發(fā)過程中需要使用C#啟動(dòng)一個(gè)外部程序(進(jìn)程),在使用完畢該外部程序后,又希望能將其關(guān)閉。我們特在此對C#啟動(dòng)和關(guān)閉外部進(jìn)程的方法進(jìn)行一個(gè)簡單的介紹。
C#啟動(dòng)外部程序(進(jìn)程)有兩種方法:一種是直接使用C#提供的Process類,利用類的函數(shù)操作來直接啟動(dòng)外部程序;另一種方法是使用傳統(tǒng)的Win32 API函數(shù)CreateProcess來實(shí)現(xiàn)外部進(jìn)程的啟動(dòng)。
使用C#提供的Process類來啟動(dòng)外部程序方法比較簡單,例舉代碼如下:
using System.Diagnostics; // 包含了Process類的定義
int myprocessID; // 進(jìn)程ID
// 方法一:直接使用.Net提供的Process類來實(shí)現(xiàn)外部程序的啟動(dòng)
private void openButton_Click(object sender, EventArgs e)
{
Process myProcess = Process.Start('\\NandFlash\\SerialTST.exe', ''); // 啟動(dòng)外部進(jìn)程
myprocessID = myProcess.Id; // 獲得該外部進(jìn)程ID
}
使用傳統(tǒng)的Win32 API函數(shù)的方法相對復(fù)雜,代碼如下:
using System.Runtime.InteropServices; // 使用外部Win32 API
#region Win32 API CreateProcess函數(shù)聲明做函數(shù)申明。
[DllImport('coredll.Dll', EntryPoint = 'CreateProcess', SetLastError = true)]
extern static int CreateProcess(string strImageName, string strCmdLine,
IntPtr pProcessAttributes, IntPtr pThreadAttributes,
int bInheritsHandle, int dwCreationFlags,
IntPtr pEnvironment, IntPtr pCurrentDir,
IntPtr bArray, ProcessInfo oProc);
public class ProcessInfo
{
public int hProcess;
public int hThread;
public int ProcessID;
public int ThreadID;
}
#endregion
方法二:使用Win32 API來實(shí)現(xiàn)外部程序的啟動(dòng)
private void openButton_Click(object sender, EventArgs e)
{
ProcessInfo pi = new ProcessInfo();
CreateProcess('\\NandFlash\\SerialTST.exe', '', IntPtr.Zero, IntPtr.Zero,
0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, pi);
myprocessID = pi.ProcessID; // 得到該程序的ID
}
關(guān)閉外部進(jìn)程的方法就是直接通過獲得的該外部進(jìn)程的ID來關(guān)閉它。這里只介紹使用.Net的Process類的方法:
// 關(guān)閉外部進(jìn)程
private void closeButton_Click(object sender, EventArgs e)
{
Process myProcessA = Process.GetProcessById(myprocessID); // 通過ID關(guān)聯(lián)進(jìn)程
myProcessA.Kill(); // kill進(jìn)程
}
上一篇:C#實(shí)現(xiàn)從多列的DataTable里取需要的幾列
欄 目:C#教程
下一篇:C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫建模的基本教程
本文標(biāo)題:簡單掌握Windows中C#啟動(dòng)外部程序進(jìn)程的方法
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6635.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)自定義windows系統(tǒng)日志的方法
- 01-10C#實(shí)現(xiàn)簡單的Login窗口實(shí)例
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10Winform消除button按下出現(xiàn)的虛線簡單實(shí)現(xiàn)方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10winform簡單緩存類實(shí)例
- 01-10C#一個(gè)簡單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#圓角窗體簡單實(shí)現(xiàn)方法
- 01-10C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10C#實(shí)現(xiàn)簡單合并word文檔的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


