我有以下字符串,我想将其作为一个进程执行:
Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m "SHARP MX-5500N PS" /h "Windows NT x86" /v 3 /f sn0hwenu.inf
但是,鉴于引号的存在,我无法在 C# 中插入此字符串以使其编译,保留所有原始结构。我应该如何解决这个问题?这有点棘手,因为字符串中有引号。
我有以下字符串,我想将其作为一个进程执行:
Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m "SHARP MX-5500N PS" /h "Windows NT x86" /v 3 /f sn0hwenu.inf
但是,鉴于引号的存在,我无法在 C# 中插入此字符串以使其编译,保留所有原始结构。我应该如何解决这个问题?这有点棘手,因为字符串中有引号。
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";
您可以将 @ 放在字符串定义的前面并放置两个"
:
string myString = @"Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m ""SHARP MX-5500N PS"" /h ""Windows NT x86"" /v 3 /f sn0hwenu.inf"
您可以在本文中阅读有关在字符串中转义字符的更多信息:
您必须使用 .转义引号\
。有一个字符串说:Hello "World"
你应该写"Hello\"World\""
string s = "Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m \"SHARP MX-5500N PS\" /h \"Windows NT x86\" /v 3 /f sn0hwenu.inf";
您也可以始终使用 Convert.ToChar(34),34 是 ".
例如:
gitInfo.Arguments = @"commit * " + "-m" + Convert.ToChar(34) + messBox.Text + Convert.ToChar(34);
等于:
commit * -m "messBox.Text";
只需\
根据需要在引号前加上反斜杠:
string yourString = "This is where you put \"your\" string";
该字符串现在包含:This is where you put "your" string