将字符串文字与DirectoryInfo枚举连接起来,并添加引号. [英] Concatenate string literals with DirectoryInfo enumeration and adding quotation marks.
问题描述
这似乎是个晦涩的问题,但是在这里:
This seems like such an obscure question, but here it goes:
当在文件路径周围添加引号时,是否有一种方法将String Literal与DirectoryInfo枚举(包含文件路径)连接起来?此外,在将DirectoryInfo枚举转换为字符串时,如何防止反斜杠加倍?我的情况如下:
Is there a way to concatenate String Literals with a DirectoryInfo enumeration (which contains a file path) while adding quotations around the file path? Further, how can I prevent backslashes from being doubled when converting a DirectoryInfo enumeration to a string? My situation is as follows:
DirectoryInfo filePathDirectory = new DirectoryInfo(filePath);
Process a = new Process();
a.StartInfo.FileName = "C:\\Windows\\system32\\lpr.exe";
a.StartInfo.Arguments = "-SServername.Domain.net -Plp " + "\"" + filePathDirectory + "\"";
a.StartInfo.UseShellExecute = false;
a.Start();
a.WaitForExit();
filePathDirectory
的起始值为:
{\\ServerName\Share\Folder\Folder\FileName.xls}
我认为,一旦连接到a.StartInfo.Arguments
并分配了以下值,该字符串便会转换为字符串:
Which I think is converted into a string once concatenated into a.StartInfo.Arguments
which is assigned the value of:
-SServername.Domain.net -Plp \"\\\\ServerName\\Share\\Folder\\Folder\\FileName.xls\"
这很糟糕,因为路径中的反斜杠数量增加了一倍.如何确保没有反斜杠添加到路径?
This is bad because, the number of backslashes in the path doubled. How can I ensure no backslashes are added to the path?
最重要的是,为了添加引号,我使用了反斜杠转义序列.但是这个转义序列中的反斜杠被无意中添加到了我的字符串中.如何在a.StartInfo.Arguments
中包含的文件路径周围添加引号?
On top of that, to add a quotation marks, I've used the backslash escape sequence; But the backslash from this escape sequence is inadvertently added to my string. How can I add quotation marks around the file path which is contained in a.StartInfo.Arguments
?
P.S.我希望这是有道理的,如果需要澄清,请提出问题.
P.S. I hope this makes sense, please ask questions if you need clarification.
推荐答案
反斜杠未加倍,引号的反斜杠也不存在".您可以通过Console.WriteLine(a.StartInfo.Arguments)
或MessageBox.Show(a.StartInfo.Arguments)
进行验证.
The backslashes are not doubled and the backslash of the quotes also "isn't there". You can verify it by Console.WriteLine(a.StartInfo.Arguments)
or MessageBox.Show(a.StartInfo.Arguments)
.
您所看到的-在我假设的调试器中-是带有转义字符 not 的字符串表示形式-就像您需要在IDE中输入它一样.
What you are seeing - in the debugger I assume - is the representation of the string with the escape characters not translated - just as you would need to enter it in the IDE.
示例:
string s = "\"";
这将在调试器中显示为"\""
,但在屏幕上显示为"
:
This will show in the debugger as "\""
but it will display on screen as "
:
这篇关于将字符串文字与DirectoryInfo枚举连接起来,并添加引号.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!