简体   繁体   English

传递参数用双引号引起来

[英]Passing argument surrounded with double quotes

I've read many solutions to this problem and have tried all of them but cannot find the correct way to accomplish this task. 我已经阅读了很多这个问题的解决方案,并尝试了所有这些解决方案,但无法找到完成此任务的正确方法。 My code is: 我的代码是:

p.StartInfo.Arguments = path;

I need the path variable to be surrounded by " marks since it is a path to a file that has spaces in the directory names and file name. How can I put a " around the start and end of the path variable? 我需要路径变量被“标记包围,因为它是一个文件的路径,在目录名和文件名中有空格。我怎样才能在路径变量的开头和结尾附近放一个? Psudo code would be: 伪代码为:

p.StartInfo.Arguments = DoubleQuote +  path + DoubleQuote;

As a followup to this situation - once my .exe file received the path - the path was in its entirety following the "\\"" suggestions. However, I had to enclose the path in the .exe file code in "\\"" so it also could find the .xlsx file since the path and file name had spaces in them. 作为这种情况的后续 - 一旦我的.exe文件收到路径 - 路径完全遵循“\\”“建议。但是,我必须将路径包含在.exe”中的.exe文件代码中它也可以找到.xlsx文件,因为路径和文件名中有空格。 Just wanted to followup with this for anyone else with this situation and wondering why the command line argument was ok, but the .exe file was not finding the file - both apps need enclosed in "\\"". 只是想对其他任何有这种情况的人进行跟进,并想知道为什么命令行参数没问题,但.exe文件没有找到文件 - 两个应用程序都需要用“\\”“括起来。

Not sure what solutions you've seen and tried but you need to escape the quotes 不确定你看到和试过的解决方案,但你需要逃避报价

p.StartInfo.Arguments = "\"" + path + "\"";

or if you want to use the verbatim string literal (use "" to escape) 或者如果你想使用逐字字符串文字(使用""来逃避)

p.StartInfo.Arguments = @""" + path + """;

If you have a lot of parameters, you might find the String.Format method easier to maintain. 如果您有很多参数,则可能会发现String.Format方法更易于维护。

p.StartInfo.Arguments = string.Format(@"""{0}""", path);

You just need to append the double quote character to the start and end of the string. 您只需要将双引号字符附加到字符串的开头和结尾。 Creating the double quote can be done in either of the following ways 创建双引号可以通过以下任一方式完成

  • "\\""
  • @""""

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

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