一般我們在開發Windows Form 桌面應用程式時,需要應用程式帶入參數啟動,以便我們得知只用者欲進行的動作或做出城市相對應的工作模式,有鑑於此我們可以透過程式碼的方式來時做Windows Form 帶入啟動參數的應用程式。

本篇文章將引導您帶參數啟動應用程式 xxxx.exe -s 並做出相對應的動作。

 

首先要使用Visual Studio 開新專案-

 

1

 

再來打開主程式的程式碼Form1.cs

 

2

 

加入以下程式碼

 

public Form1()
{
    InitializeComponent();
    MessageBox.Show("正常啟動應用程式");
}
//新加入的
public Form1(string parameter)
{
    InitializeComponent();
    if (parameter == "-s") { 
        //若傳入的參數為-s
        MessageBox.Show("傳入的參數是-s");
    }
    if (parameter == "-p")
    {
        //若傳入的參數為-p
        MessageBox.Show("傳入的參數是-p");
    }
}

 

接著打開專案底下的 Program.cs

 

3

 

修改程式碼如下:

 

static class Program
{
    /// <summary>
    /// 應用程式的主要進入點。
    /// </summary>
    [STAThread]
    static void Main(String[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Form1());
        if (args.Length == 0)
        {
            //若沒有傳入參數則正常啟動
            Application.Run(new Form1());
        }
        else
        {
            //若有傳入參數進入自定義的啟動模式
            Application.Run(new Form1(args[0].ToString()));
        }
 
    }
}

 

接著我們可以測試有沒有正常運作囉,運行!

首次運行後會跳出視窗

4

 

因為我們還沒帶入參數所以是"正常啟動應用程式"

 

但我們可以到程式的目錄資料夾去新增捷徑,來帶入參數啟動!!

 

5

 

建立捷徑時要帶入參數"-s" 或 “-p"

 

 

 

 

 

 

 

 

 

 

6

 

如此一來在運行的時候就會出現

 

7

 

若參數改為"-p" 則會出現

 

8

 

 

如此一來我們便可以使用參數啟動應用程式囉!

 

References : [C#] 啟動應用程式並且傳入參數

 

文章中的敘述如有觀念不正確錯誤的部分,歡迎告知指正 謝謝
http://www.sukitech.com/?p=915
轉載請註明出處,並且附上本篇文章網址 !  感謝。

86 Thoughts on “C# Windows Form 開發,帶參數啟動應用程式 xxxx.exe -s 並做出相對應的動作。

  1. sul lavoro, sicurezza alimentare, ambiente, privacy, servizi di Audit.

  2. réseaux informatique

  3. You have touched some fastidious factors here

  4. I loved your post. Really Great.

  5. important source

  6. inka express bus

  7. floristeria en Argentina

  8. floristeria en Argentina

  9. wow, awesome blog post.

  10. best instagram bot

  11. I cannot thank you enough for the blog.Really thank you! Will read on…

  12. I think this is a real great blog article. Want more.

  13. I value the article.Really looking forward to read more. Really Cool.

  14. I loved your article post.Really looking forward to read more. Cool.

  15. Major thankies for the article.Really thank you! Awesome.

  16. Bodo nyheter

  17. Very informative blog.Thanks Again. Much obliged.

  18. Thanks a lot for the blog article. Want more.

  19. Thanks for the article post.Really thank you! Much obliged.

  20. Very informative blog.Thanks Again. Want more.

  21. free hulu accounts 2017

  22. Very neat post.Much thanks again. Keep writing.

  23. Awesome post.Thanks Again.

  24. Thanks-a-mundo for the article post.Much thanks again. Fantastic.

  25. wow, awesome post.Much thanks again. Will read on…

  26. Thank you ever so for you blog article.Much thanks again.

  27. Im obliged for the blog article. Really Great.

  28. Thanks-a-mundo for the article.Much thanks again. Keep writing.

  29. wow, awesome blog article.Much thanks again. Really Cool.

  30. I really like and appreciate your article.Much thanks again. Keep writing.

  31. Really enjoyed this blog article. Really Great.

  32. Very good article post.Really thank you! Really Great.

  33. Really enjoyed this blog.Really thank you! Fantastic.

  34. This is one awesome article.

  35. I really enjoy the blog article.Much thanks again. Will read on…

  36. Wow, great article post.Really thank you! Really Great.

  37. Enjoyed every bit of your blog article. Keep writing.

  38. Thanks for the article post. Cool.

  39. Appreciate you sharing, great article post.Really thank you! Awesome.

  40. Great, thanks for sharing this article.Really looking forward to read more.

  41. Fantastic blog post.Really looking forward to read more. Want more.

  42. Thanks again for the post. Really Great.

  43. Great article.Really thank you! Will read on…

  44. Great, thanks for sharing this article post.Much thanks again. Will read on…

  45. I really liked your article post. Keep writing.

  46. Thanks for the post.Really thank you! Really Cool.

  47. Make raw foods, especially raw vegetables, a cornerstone of any weight-loss diet. Cooking typically leaches vital nutrients and vitamins out of food. These losses are most evident with vegetables. Beyond the technical, nutritional benefits, a dieter will likely find that raw, fresh foods taste better than their canned, preserved or cooked, counterparts.

  48. I loved your blog post.Thanks Again. Much obliged.

  49. I am so grateful for your article post.Really looking forward to read more. Great.

  50. A round of applause for your blog post.Thanks Again. Awesome.

  51. Great, thanks for sharing this blog post.Really thank you! Really Cool.

  52. Very neat post.Thanks Again.

  53. You don’t really have to pay charges for calling information.You can dial 1-800-411-FREE.You will be able to get the information you need after an advertisement.

  54. Skip unneeded extras you don’t need when buying a cell phone.

  55. Don’t go overboard with SEO keywords, ads, images, or keywords. Write naturally, natural and flowing style.

  56. I cannot thank you enough for the blog.Thanks Again. Fantastic.

  57. I really like and appreciate your post.Really looking forward to read more. Really Cool.

  58. |Bookmarked your page

  59. Hey, thanks for the article.Thanks Again. Really Cool.

  60. Enjoyed every bit of your article post. Cool.

  61. Great, thanks for sharing this blog.Really looking forward to read more.

  62. Thanks a lot for the blog post.Really thank you! Keep writing.

  63. Very informative blog.Much thanks again. Much obliged.

  64. Im thankful for the blog article.Really thank you! Awesome.

  65. I really like and appreciate your post.Really thank you! Want more.

  66. Thanks for the blog.Thanks Again. Awesome.

  67. Appreciate you sharing, great blog article.Thanks Again. Will read on…

  68. A round of applause for your article.Really thank you! Really Cool.

  69. I appreciate you sharing this article.Thanks Again.

  70. A big thank you for your blog.Really thank you! Want more.

  71. Hey, thanks for the article.Really thank you! Fantastic.

  72. Great, thanks for sharing this article. Much obliged.

  73. I appreciate you sharing this blog article.Really looking forward to read more.

  74. wow, awesome post.Much thanks again. Great.

發表迴響

你的電子郵件位址並不會被公開。

Post Navigation