C#帶參數運行方法

來源:互聯網
上載者:User
比如
aa.exe -auto
aa.exe -main
兩組尾碼,要求分別運行aa的某個線程,比如aa.exe -auto開啟from1,aa.exe -main開啟from2
由於需要修改Program的Main方法,需要更加謹慎,因為一個結構清晰的Main對於後期維護是一個很好的協助。以下的代碼將解析參數,構造啟動表單,啟動表單三個邏輯分割為三個方法Code
 1 static class Program
 2     {
 3         /// <summary>
 4         /// The main entry point for the application.
 5         /// </summary>
 6         [STAThread]
 7         static void Main(string[] Args)
 8         {
 9 
10             Application.EnableVisualStyles();
11             Application.SetCompatibleTextRenderingDefault(false);
12             //啟動有預設啟動表單構造器構造出來的啟動表單
13 Application.Run(StartFormCreator(ParseArgsForFormlabel(Args)));
14         }
15 
16 //從參數中解析啟動表單參數
17         static string ParseArgsForFormlabel(string[] args)
18         {
19             string formLable = string.Empty;
20 //如果參數數量大於0則截取第一個參數,否則傳回值為string.Empty
21             if (args.Length > 0)
22             {
23                 formLable = args[0];
24             }
25             return formLable;
26         }
27 //根據啟動表單參數構造對應的表單
28         static Form StartFormCreator(string Label)
29         {
30 //如果參數是-auto則構造Form1,否則為Form2
31             if (Label.ToLower() == "-auto")
32             {
33                 return new Form1();
34             }
35             else
36             {
37                 return new Form2();
38             }
39         }
40     } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.