扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

C#浣跨敤WinExec璋冪敤exe绋嬪簭

扬州沐宇科技
2023-12-19 05:51:48
c#, WinExec

鍦–#涓娇鐢╓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>

扫码添加客服微信