Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

How to receive execution arguments on a C# WinForms Application?

, 24 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
The below code that illustrates how to receive arguments for a WinForms Application.The example expects two arguments and validates that the are converted properly. If the appropiate argumetns are sent, these are stored in a public class named GlobalVars.[STAThread] static void Main( string[] MyArgs

Editorial Note

This article appears in the Third Party Product Reviews section. Articles in this section are for the members only and must not be used by tool vendors to promote or advertise products in any way, shape or form. Please report any spam or advertising.

The below code that illustrates how to receive arguments for a WinForms Application. The example expects two arguments and validates that the are converted properly. If the appropiate argumetns are sent, these are stored in a public class named GlobalVars.
[STAThread]
 static void Main( string[] MyArgs)
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);           

     if (MyArgs.Length == 0 || MyArgs.Length < 2)
     {
        MessageBox.Show(
        "Please specify Parm1 and Parm2 as Arguments in order to
         Proceed.", 
        "YourAppTitle", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Application.Exit();
        return;
      }
      else
      {
         try
         {
            GlobalVars lGlobalVars = new GlobalVars();  
            lGlobalVars.gOrderSkey = Int64.Parse(MyArgs[0]);
            lGlobalVars.gShipmentNo = Int64.Parse(MyArgs[1]);
          }
          catch (Exception Ex)
          {
             MessageBox.Show(
          "Invalid Parm1 and/or Parm2 Arguments. Please verify. \rError: " 
          + Ex.Message, "YourAppTitle", 
          MessageBoxButtons.OK, MessageBoxIcon.Error);
          Application.Exit();
          return;
          }
          Application.Run(new Form1());    
      }

}
Notice the "\n" to break down the MessageBox in two lines. Is a nice little trick that I often forget

Hope this helps,
Will

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

gmtzgtz

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 24 Nov 2011
Article Copyright 2011 by gmtzgtz
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid