34

I have the following string, which I want to execute as a process:

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

However, given the presence of quotation marks, I can't insert this string in C# to make it compile, keeping all of the original structure. How should I fix this? It's a little tricky as there are quotation marks within the string.

3

7 Answers 7

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

or

string whatever = @"Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m ""SHARP MX-5500N PS"" /h ""Windows NT x86"" /v 3 /f sn0hwenu.inf";
Sign up to request clarification or add additional context in comments.

Comments

20

You can put @ in front of the string definition and put two ":

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

You can read more about escaping characters in strings in this article:

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

Comments

5

you have to escape the quotation marks using \. to have a string that says: Hello "World" you should write "Hello\"World\""

Comments

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

Comments

2

You can also always use Convert.ToChar(34), 34 being the ASCII of ".

for eg:

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

equals:

commit * -m "messBox.Text";

Comments

1

Starting with C# 11, it is possible to use three quotes (""") at the beginning and end of a string, instead of just one as usual:

string s = """{ "Id": 1, "Name": "Alex" }""";

Comments

0

Just put in a backslash \ before the quotation marks as needed:

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

The string now contains: This is where you put "your" string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.