Results 1 to 4 of 4

Thread: Newbie in C# Using Agruments

  1. #1

    Thread Starter
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    Newbie in C# Using Agruments

    Hi I need to call my Program like this:
    C:\program.exe name

    And I want to show the Agrument name in a label.
    I tryed to do this like this:

    Code:
    namespace HelloWorld
    {
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }
    
            private void frmMain_Load(object sender, EventArgs e)
            {
                lblMsg.Text = "Hello" + e + "to this world!";
            }
        }
    }
    I figured out that this was wrong..
    I didn't find anything on google.
    And I asked my C# Teacher.. And she had NO CLUE were I was talking about..

    (Ok, she is really a noob, she only knows or textbook inside - out but ok..)

    Is here ayone how can help me out ^^?

    506C65617365205261746520506F7374732E2E2E

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Newbie in C# Using Agruments

    The command-line arguments are not sent to any events, they are sent to the static Main() procedure of your startup class, if you define one that has a string[] argument.

    Code:
    class MyProgram
    {
      [STAThread] static void Main(string[] args)
      {
        // ...
      }
    }
    You can also access them using the static Environment.GetCommandLineArgs() method.

    Code:
    lblMsg.Text = "Hello" + string.Join(", ", Environment.GetCommandLineArgs()) + "to this world!";

  3. #3

    Thread Starter
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    Re: Newbie in C# Using Agruments

    So in Program.cs I need to change:
    Code:
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmMain());
            }
    into
    Code:
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmMain());
            }
    this

    And then I need to send the var args to my Form1.cs?

    Thanks ^^

    506C65617365205261746520506F7374732E2E2E

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Newbie in C# Using Agruments

    Yeah you can do that by passing them to the frmMain constructor or you can use Environment.CommandLine like I showed in my second example. Whichever way you choose is up to you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width