執行外部執行檔 (C#)
跳至導覽
跳至搜尋
使用Win32 API
WinExec
//定義 [DllImport("kernel32.dll")] static extern uint WinExec(string lpCmdLine, uint uCmdShow); //使用 uint SW_HIDE = 0; uint SW_SHOW = 5; string strFilename ="notepad.exe"; WinExec(strFilename, SW_SHOW);
CreateProcess
- pinvoke.net: CreateProcess (kernel32)
- 常配合 WaitForSingleObject 與 CloseHandle 使用
- Accessing CreateProcess from C# and VB.NET
- 使用c#调用API函数CreateProcess来运行外部程序
- Launch an Executable Programatically on Windows Mobile using CreateProcess and C#
ShellExecute
使用ActiveX
ActiveX的用法常因為用於病毒或木馬的入侵管道(以VBScript或JavaScript去Call外部的exe檔),所以常被安全性設定或掃毒軟體所封鎖。
Shell.Application
using Shell32; private Integer ShellExecute(string strCmd) { Type shellAppType = Type.GetTypeFromProgID("Shell.Application"); var shell = Activator.CreateInstance(shellAppType); return Shell.ShellExecute(strCmd); }
使用.NetFramework
System.Diagnostics.Process.Start
- 範例:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.FileName = "notepad.exe"; psi.Arguments = "c:\\1.txt"; System.Diagnostics.Process.Start(psi);