Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to escape backslash in a config file?

I have a config file, myapp.exe.config. In the file I have an attribute with a fullpath filename as the value.

<add key="InfoFile" value="c:\temp\info.txt" /> 

It seems to work if I use a single or double backslash. That is,

<add key="InfoFile" value="c:\\temp\\info.txt" /> 

works also. What is the correct way to do this?

like image 317
RogerS Avatar asked Apr 16 '11 08:04

RogerS


People also ask

How do you escape a slash in path?

About Escaping File Paths Characters to be escaped include the backslash (\, because it is the escaping character) and the double quotes ("). Both of these characters can be relevant in file paths.

What does escape backslashes mean in R?

In R (and elsewhere), the backslash is the “escape” symbol, which is followed by another symbol to indicate a special character. For example, "\t" represents a “tab” and "\n" is the symbol for a new line (hard return).

How do you escape a backslash in C#?

If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.


1 Answers

You don't need that. Anything within an attribute value is character data.

Since you're reading these values using C#, they'll get escaped as if they would be a literal path string in code.

Anyway, you might want to know that C# has @ operator to declare verbatim strings, meaning that you don't need to escape backslashes when using literal paths in code:

string somePath = @"C:\blah\blih\bluh.txt"; 
like image 154
Matías Fidemraizer Avatar answered Sep 20 '22 05:09

Matías Fidemraizer