问题描述
我有一个配置文件 myapp.exe.config.在文件中,我有一个以完整路径文件名作为值的属性.
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.
由于您使用 C# 读取这些值,因此它们将被转义,就好像它们是代码中的文字路径字符串一样.
Since you're reading these values using C#, they'll get escaped as if they would be a literal path string in code.
无论如何,您可能想知道 C# 有 @
运算符来声明逐字字符串,这意味着在代码中使用文字路径时不需要转义反斜杠:
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";
这篇关于我需要在配置文件中转义反斜杠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!