31

我有以下字符串,我想将其作为一个进程执行:

Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m "SHARP MX-5500N PS" /h "Windows NT x86" /v 3 /f sn0hwenu.inf

但是,鉴于引号的存在,我无法在 C# 中插入此字符串以使其编译,保留所有原始结构。我应该如何解决这个问题?这有点棘手,因为字符串中有引号。

4

6 回答 6

39
string whatever = "Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m \"SHARP MX-5500N PS\" /h \"Windows NT x86\" /v 3 /f sn0hwenu.inf";

或者

string whatever = @"Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m ""SHARP MX-5500N PS"" /h ""Windows NT x86"" /v 3 /f sn0hwenu.inf";
于 2011-04-04T15:29:35.233 回答
18

您可以将 @ 放在字符串定义的前面并放置两个"

string myString = @"Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m ""SHARP MX-5500N PS"" /h ""Windows NT x86"" /v 3 /f sn0hwenu.inf"

您可以在本文中阅读有关在字符串中转义字符的更多信息:

http://www.yoda.arachsys.com/csharp/strings.html

于 2011-04-04T15:29:08.530 回答
5

您必须使用 .转义引号\。有一个字符串说:Hello "World"你应该写"Hello\"World\""

于 2011-04-04T15:29:04.323 回答
4
string s = "Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m \"SHARP MX-5500N PS\" /h \"Windows NT x86\" /v 3 /f sn0hwenu.inf";
于 2011-04-04T15:28:30.987 回答
1

您也可以始终使用 Convert.ToChar(34),34 是 ".

例如:

gitInfo.Arguments = @"commit * " + "-m" + Convert.ToChar(34) + messBox.Text + Convert.ToChar(34);

等于:

commit * -m "messBox.Text";
于 2017-08-29T10:46:59.780 回答
0

只需\根据需要在引号前加上反斜杠:

string yourString = "This is where you put \"your\" string";

该字符串现在包含:This is where you put "your" string

于 2021-04-14T19:37:04.650 回答