views:

756

answers:

3

Hi guys,

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?

+3  A: 

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.

Jon Skeet
John that's cheating...getting in first :-) but only giving a half answer first time around :-)
Michael Prewecki
:-) already in there before my comment :-)
Michael Prewecki
@Michael: I had the program, but it was on a different computer. I had to answer + edit to get it all in there :)
Jon Skeet
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.
balexandre
+2  A: 

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

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
Sani Huttunen