我需要在配置文件中转义反斜杠吗? [英] Do I need to escape backslash in a config file?

查看:411
本文介绍了我需要在配置文件中转义反斜杠吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置文件myapp.exe.config。
在文件中,我有一个带有fullpath文件名的属性作为值。

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

如果我使用单一或双重反斜杠,这似乎是有效的。也就是说,

 < add key =InfoFilevalue =c:\\temp\\\ .txt/> 

也可以。什么是正确的方法?

解决方案

你不需要。属性值中的任何内容都是字符数据。



由于您使用C#读取这些值,所以它们将被转义,就像它们是代码中的文字路径字符串一样



无论如何,你可能想知道C#有 @ 运算符来声明逐字字符串,这意味着你在代码中使用文字路径时不需要转义反斜杠:

  string somePath = @C:\blah\blih \bluh.txt; 


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

这篇关于我需要在配置文件中转义反斜杠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆