简体   繁体   English

将文件路径传递给Main(string [] args)不起作用

[英]Passing filepath into Main(string[] args) not working

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. 我正在尝试将文件路径传递到C# 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: 弹出一个Windows对话框并通知我我的应用程序已停止工作,当我单击Debug选项时,我可以看到args[0]的结果是:

C:\\Users\\DevDave\\Documents\\Visual Studio 2012\\Projects\\MyProject" 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: 我再次收到错误,在调试中查看后,我看到args [0]是:

C:\\Users\\DevDave\\Documents\\Visual Studio 2012\\Projects\\MyProject" any 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. 我只想传入当前解决方案目录的文件路径,并使用$(SolutionDir)从预构建事件调用我的应用程序,并且知道我可以通过其他方式获取当前解决方案的路径。 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: 或者,更简单,但你必须以某种方式在C#中处理它:

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 / 看一下: http//en.wikipedia.org/wiki/Path_(computing) ,你可以同时使用\\/ in路径但如果你想要任何shell兼容性我建议使用/

Anything that comes after MyApp will be the first argument (args[0]). MyApp之后的任何内容都将成为第一个参数(args [0])。 In your case, the first argument is "C:\\Users\\DevDave\\Documents\\Visual Studio 2012\\Projects\\MyProject\\" . 在您的情况下,第一个参数是"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 此外,字符串末尾的引号似乎发生,因为\\"意味着你想要转义引号并将其写为字符串。在这种情况下,引用不是关闭字符串。这就是为什么你的args [0]是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: 继续Henk的回答,并选择在路径的末尾添加\\ ,然后:

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: 如果您选择在Henk的答案中选择此代码来破解错误(“更简单,但您必须以某种方式在C#中处理它”),那么您应该意识到会发生一些错误:

  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. args [0]只会被设置,即使你传递了多个参数.args的长度将等于1.所以你必须将args [0]分成多个部分供你的hack使用。

  2. You have to replace any " characters with a \\ if they are at the end of the pieces you split. 如果它们位于您分割的片段的末尾,则必须将所有"字符替换为\\ "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM