balexandre
balexandre

Reputation: 75083

passing args (arguments) into a window form application

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?

Upvotes: 3

Views: 14606

Answers (3)

Sani Huttunen
Sani Huttunen

Reputation: 24385

How are you executing your application?
If you execute it from another application you might have forgotten to format the argument string properly:

String arguments = "First \"Sec ond\" Third Fourth \"Fi fth\""

would have five arguments whereas

String arguments = "First Sec ond Third Fourth Fi fth"

would have seven.

If the arguments are in the shortcut target property then the same applies:

"C:\My Path\MyApplication.exe" "Argument 1" Argument2 Argument3 "Argument 4"

instead of

"C:\My Path\MyApplication.exe" Argument 1 Argument2 Argument3 Argument 4

Upvotes: 0

Xn0vv3r
Xn0vv3r

Reputation: 18184

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]);
        }
    }
}

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500515

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.

Upvotes: 6

Related Questions