溫馨提示×

C#中子進程的啟動參數設置

c#
小樊
132
2024-08-27 10:03:10
欄目: 編程語言

在C#中,你可以使用System.Diagnostics.Process類來啟動子進程并設置其啟動參數

using System;
using System.Diagnostics;

namespace SubprocessExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創建一個新的ProcessStartInfo對象
            ProcessStartInfo startInfo = new ProcessStartInfo();
            
            // 設置子進程的可執行文件路徑
            startInfo.FileName = "your_executable_file.exe";
            
            // 設置子進程的啟動參數
            startInfo.Arguments = "arg1 arg2 arg3";
            
            // 如果需要,可以設置其他屬性,例如工作目錄、窗口樣式等
            startInfo.WorkingDirectory = "path_to_working_directory";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            
            // 使用ProcessStartInfo對象啟動子進程
            Process process = Process.Start(startInfo);
            
            // 等待子進程退出
            process.WaitForExit();
            
            // 獲取子進程的退出代碼
            int exitCode = process.ExitCode;
            Console.WriteLine($"Subprocess exited with code: {exitCode}");
        }
    }
}

在這個示例中,我們首先創建了一個ProcessStartInfo對象,然后設置了子進程的可執行文件路徑和啟動參數。接下來,我們使用Process.Start()方法啟動子進程,并等待其退出。最后,我們獲取子進程的退出代碼并將其輸出到控制臺。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女