Results 1 to 6 of 6

Thread: [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Resolved [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program

    Anyone know how to pass a startup parameter to a program in C#.
    I searched the net all over and the only thing I could find was for console applications. I also looked for overloads for the Form1_Load() and didnt find anything.

    I would like to pass options to my program such as

    program.exe /a
    program.exe /b
    program.exe /c
    etc...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How To Pass A Startup Parameter To A Windows C# Program

    You pass them exactly has you have shown when starting the program. To read them within your app you would use the Environment.GetCommanLineArgs method.
    Code:
    string[] args = Environment.GetCommandLineArgs();
    
    // The first commandline argument is always the executable path itself.
    if (args.Length > 1)
    {
        if (Array.IndexOf(args, "/a") != -1)
        {
            // The "/a" switch was used.
        }
    
        if (Array.IndexOf(args, "/b") != -1)
        {
            // The "/b" switch was used.
        }
    
        if (Array.IndexOf(args, "/c") != -1)
        {
            // The "/c" switch was used.
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: How To Pass A Startup Parameter To A Windows C# Program

    cool thanks alot

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How To Pass A Startup Parameter To A Windows C# Program

    Don't forget to resolve your thread from the Thread Tools menu.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program

    done. thanks again

  6. #6
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program

    Quote Originally Posted by 2MuchRiceMakesMeSick
    I also looked for overloads for the Form1_Load() and didnt find anything.
    If you start the program with the "Main" function instead of Form1_Load, you can get the command line args from there:

    Code:
    public string myProgramArgs;
    
    static void Main(string[] args)
    {
        myProgramArgs = args;
        Application.Run(new Form1);
    }
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

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