passing args (arguments) into a window form application

14,095

Solution 1

Are you running it directly from the command line? If so, I'd expect that to work just fine. (I assume you're using the parameters from the Main method, by the way?)

For instance, here's a small test app:

using System;

class Test
{
    static void Main(string[] args)
    {
        foreach (string arg in args)
        {
            Console.WriteLine(arg);
        }
    }
}

Execution:

>test.exe first "second arg" third
first
second arg
third

This is a console app, but there's no difference between that and WinForms in terms of what gets passed to the Main method.

Solution 2

MSDN says, that it should work the way you mentioned.

class CommandLine
{
    static void Main(string[] args)
    {
        // The Length property provides the number of array elements
        System.Console.WriteLine("parameter count = {0}", args.Length);

        for (int i = 0; i < args.Length; i++)
        {
            System.Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
        }
    }
}
Share:
14,095
balexandre
Author by

balexandre

Father (x3), Husband (x1), Dedicated, Friendly. Web, Windows and Mobile developer (mostly .NET and NodeJs). Photographer by hobby, Windsurfer by passion. Born in Portugal (in 1977),happily married to a beautiful Romanian gal (since 2005) and living in Denmark (since 2006) 😊 P.S. My Simpson character was designed by a Fiverr.com user.

Updated on June 04, 2022

Comments

  • balexandre
    balexandre about 2 years

    I have my Windows Application that accepts args and I use this in order to set up the Window behaviour

    problem is that I need to pass text in some of this arguments but my application is looking at it as multiple args, so, this:

    "http://www.google.com/" contact 450 300 false "Contact Info" true "Stay Visible" true
    

    has actually 11 arguments instead of the 9 that I am expecting.

    What is the trick to get "contact info" and "stay visible" to be passed as only one argument?

  • Michael Prewecki
    Michael Prewecki over 15 years
    John that's cheating...getting in first :-) but only giving a half answer first time around :-)
  • Jon Skeet
    Jon Skeet over 15 years
    @Michael: I had the program, but it was on a different computer. I had to answer + edit to get it all in there :)
  • balexandre
    balexandre over 15 years
    I was actually passing \"Stay connect\" :( - I used copy/paste into the build parameters... maybe this is why iPhone does not have copy(paste function ;) Thank you Jon.