現有個需求是通過一個主程序獲取配置的線程數和進程數打開連一個控制台程序,將線程數和系統編碼作為參數傳給控制台程序。
下面附上Demo。
1 private static void Main(string[] args) 2 { 3 var arg = new string[2]; 4 arg[0] = "5";//線程數 5 arg[1] = "SystemCode";//系統編碼 6 7 //打開指定目錄地址 8 //StartProcess(@"E:\Demo\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe", arg); 9 10 for (int i = 1; i < 2; i++) 11 { 12 Console.WriteLine("123"); 13 StartProcess("ConsoleApplication1.exe", arg); 14 } 15 } 16 17 public static bool StartProcess(string filename, string[] args) 18 { 19 try 20 { 21 string s = ""; 22 foreach (string arg in args) 23 { 24 s = s + arg + " "; 25 } 26 s = s.Trim(); 27 var myprocess = new Process(); 28 var startInfo = new ProcessStartInfo(filename, s); 29 myprocess.StartInfo = startInfo; 30 //通過以下參數可以控制exe的啟動方式,具體參照 myprocess.StartInfo.下面的參數,如以無界面方式啟動exe等 31 //myprocess.StartInfo.UseShellExecute = true; 32 myprocess.Start(); 33 return true; 34 } 35 catch (Exception ex) 36 { 37 Console.WriteLine("啟動應用程序時出錯!原因:" + ex.Message); 38 } 39 return false; 40 }