开发者

Do I need to escape backslash in a config file?

开发者 https://www.devze.com 2023-02-25 01:09 出处:网络
I have a config file, myapp.exe.config. In the file I have an attribute with a fullpath filename as the value.

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?


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";


A backslash has no special meaning in XML, so they should not be escaped.

Besides, if you would escape the backslashes in XML you would not use \\, you would use &#92;.

The reason that it works with double backslashes also is that the file system is forgiving. You can use the path c:\\temp\\info.txt to reach the file c:\temp\info.txt.


Basically URL or URI holds single slash \ so, its better to use single slash. The problem comes while writing code, but in XML there is no problem to use single slash.


I think the best would to prevent the double backslash just in case, but if it works why change it. Maybe replace "\\" with "\" when you read the config value into your application.

0

精彩评论

暂无评论...
验证码 换一张
取 消