C#浣跨敤WinExec璋冪敤exe绋嬪簭
鍦–#涓娇鐢╓inExec鏉ヨ皟鐢╡xe绋嬪簭鍙互閫氳繃浣跨敤DllImport鏉ュ鍏inExec鍑芥暟锛岀劧鍚庤皟鐢ㄥ畠鏉ユ墽琛屾寚瀹氱殑exe绋嬪簭銆?/p>
棣栧厛锛岄渶瑕佸湪浠g爜涓鍏ystem.Runtime.InteropServices鍛藉悕绌洪棿锛屼互渚夸娇鐢―llImport鐗规€с€?/p>
using System;
using System.Runtime.InteropServices;
public class Program
{
[DllImport("kernel32.dll")]
public static extern int WinExec(string fileName, int command);
public static void Main()
{
string path = "C:\\path\\to\\your\\program.exe";
int result = WinExec(path, 1);
if (result > 31)
{
Console.WriteLine("The program was launched successfully.");
}
else
{
Console.WriteLine("Failed to launch the program.");
}
}
}
鍦ㄤ笂闈㈢殑绀轰緥涓紝鎴戜滑閫氳繃璋冪敤WinExec鍑芥暟鏉ユ墽琛屾寚瀹氱殑exe绋嬪簭銆俉inExec鍑芥暟鐨勭涓€涓弬鏁版槸瑕佹墽琛岀殑绋嬪簭鐨勮矾寰勶紝绗簩涓弬鏁版槸鍛戒护閫夐」銆傚湪杩欓噷锛屾垜浠皢鍛戒护閫夐」璁剧疆涓?锛岃〃绀烘樉绀虹▼搴忕獥鍙c€?/p>
WinExec鍑芥暟鐨勮繑鍥炲€兼槸涓€涓暣鏁帮紝濡傛灉澶т簬31锛屽垯琛ㄧず绋嬪簭鍚姩鎴愬姛銆傚湪涓婇潰鐨勭ず渚嬩腑锛屾垜浠鏌ヨ繑鍥炲€煎苟鎵撳嵃鐩稿簲鐨勬秷鎭€?/p>
璇锋敞鎰忥紝WinExec鍑芥暟宸茬粡琚玀icrosoft鏍囪涓鸿繃鏃剁殑鍑芥暟銆傚湪鏂扮殑C#鐗堟湰涓紝鎺ㄨ崘浣跨敤Process.Start鏂规硶鏉ュ惎鍔ㄥ閮ㄧ▼搴忋€?/p>