Transferring the path to the Main file (string [] args) does not work

I am trying to pass the file path to the C# Console Application , but I am having problems with the wrong string by the time the console application is reached.

If I started the application from the command line with the file path parameter:

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

A Windows dialog box will appear and inform that my application has stopped working, and when I click the Debug option, I see that the result of args[0] is:

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

Note that there is still a trailing quote at the end.

If I pass the second argument:

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

I get the error message again, and after looking in the debugging, I see that args [0]:

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

I'm confused why this is happening. My only assumption is that the backslash in the string causes some sort of escape sequence from the string? Change I noticed that the same thing happens in the above line example! It seems to be having problems here.

I just want to transfer the file path of the current solution directory and call my application from the pre-build event using $ (SolutionDir), and I know that I can get the path to the current solution in other ways. But this is easiest, and I am curious why it is not working properly.

+4
source share
4 answers

Yes, the rules for command line arguments are a little muddy.

\ is an escape char, and you can use it to return quotes ( " ). You will have to avoid the backslash, but only when it precedes the quote. So use (note the" \\ "at the end):

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

or, easier, but you have to deal with it in C # anyway:

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

Also see this question

+5
source

Therefore it is always better to use / in transit

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

look: http://en.wikipedia.org/wiki/Path_(computing) , you can use both \ and / in the path, but if you want some kind of shell compatibility I suggest using /

+1
source

Everything 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 line seems to be because \" means you want to avoid the quote and write it as a string. In this case, the quote does not close the line. That's why your [0] arguments are all that comes after MyApp

If you do not want to view the quote and have a slash, you must do \\"

You can try this and tell me what will happen:

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

(look at the double slash)

Hope this helps.

+1
source

Continue with Henk's answer and select add \ at the end of the path, and then:

If you decide to crack the error by selecting this code in Henk’s answer ("easier, but you have to deal with it in C # somehow:"), then you should understand some errors that will occur:

  • args [0] will be set even if you pass several parameters. The length of the arguments will be 1. Thus, you need to split args [0] into several parts for your hack.

  • You must replace any characters " with \ if they are at the end of the separated fragments.

0
source

Source: https://habr.com/ru/post/1493155/


All Articles