Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple args in Console Application not parsing correctly

Easy to reproduce but realy strange to me:

Add the following 'args' with 3 strings into the Command line arguments Textbox in VisualStudio (Under Project Properties->Debug->Start Options):

-SourceFile:"c:\temp\file.txt" -DestinationFolder:"c:\temp\" -ArchiveFolder:"C:\temp\"

Test it with this simple Console Application:

class Program
{
    static void Main(string[] args)
    {
        foreach (string t in args)
        {
            Console.WriteLine(t);
        }
        Console.ReadKey();
    }
}

Result: the array (args[]) has 2 instead of 3 strings?

[0] SourceFile:c:\temp\file.txt
[1] DestinationFolder:c:\temp" -ArchiveFolder:C:\temp"

Can someone explain me why this happens? There is something strange with the quotes cause normaly, the quotes will be removed by .net, but here, there are still some quotes... but I can't see the problem...

Thanks for any help!

like image 391
dataCore Avatar asked Jan 22 '13 17:01

dataCore


People also ask

Why do we use string args in main method in C#?

String []args: For accepting the zero-indexed command line arguments. args is the user-defined name. So you can change it by a valid identifier.

What is args in C#?

The args parameter stores all command line arguments which are given by the user when you run the program. If you run your program from the console like this: program.exe there are 4 parameters. Your args parameter will contain the four strings: "there", "are", "4", and "parameters"


1 Answers

You have a \" in the DestinationFolder value, which "escapes" the quote, including it in the text of the value rather than it pairing with the opening quote to close the string. You want a literal, \, so use \\:

-SourceFile:"c:\temp\file.txt" -DestinationFolder:"c:\temp\\" -ArchiveFolder:"C:\temp\\"

(you can even see the escaping in action in SO's highlighting engine)

like image 140
kevingessner Avatar answered Oct 13 '22 11:10

kevingessner