Вопрос

I am trying to pass a file path into a C# Console Application but am having problems with the string being incorrect by the time it reaches the console application.

If I run my application from the command line, with a file path parameter:

MyApp "C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject\"

A windows dialogue pops up and informs me that my application has stopped working, and when I click the Debug option, I can see that the result of args[0] is:

C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject"

Note there is still a trailing quote at the end.

If I pass a second argument:

MyApp "C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject\" "any old string"

I get an error again, and after viewing in debug I see that args[0] is:

C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject" any

I am baffled as to why this is happening. My only guess is that the backslashes in the string are causing some kind of escape sequence from the string? Edit: I notice that the same is happening in the string example above! It seems \" is causing problems here.

I just want to pass in the file path of the current solution directory and am calling my app from a pre-build event using $(SolutionDir), and know that I can get the path of the current solution in other ways. But this is simplest and I am curious as to why it does not work as expected.

Это было полезно?

Решение

Yes, the rules for commandline arguments are a little murky.

The \ is the escape char and you can use it to escape quotes ("). You'll have to escape a backslash but only when it is preceding a quote. So use (note the '\\' at the end):

MyApp "C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject\\" 

or, simpler but you'll have to deal with it in C# somehow:

MyApp "C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject" 

Also see this question

Другие советы

That's why it's always better to use / in path

"C:/Users/DevDave/Documents/Visual Studio 2012/Projects/MyProject/" "any old string"

take a look: http://en.wikipedia.org/wiki/Path_(computing) , you can use both \ and / in path but if you want any shell compatibility I suggest to use /

Anything that comes after MyApp will be the first argument (args[0]). In your case, the first argument is "C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject\". Also, the quote at the end of the string seems to happen because \" means that you want to want to escape the quote and write it as a string. In this case, the quote is not closing the string. That is the reason that your args[0] is the whole thing that comes after MyApp

If you don't want to scape the quote and have a slash behind it, you should do \\"

You could try this and tell me what happens:

MyApp "C:\Users\DevDave\Documents\Visual Studio 2012\Projects\MyProject\\"

(look at the double slash)

Hope it helps.

Continuation of Henk's answer, and choose to add a \ at the end of the path, then:

If you choose to hack the bug by choosing this code in Henk's answer ("simpler but you'll have to deal with it in C# somehow:"), then you should realize some bugs that will occur:

  1. args[0] will only be set, even if you pass multiple parameters in. The length of args will be equal to 1. So you have to split args[0] into multiple pieces for your hack.

  2. You have to replace any " characters with a \ if they are at the end of the pieces you split.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top