5

是否可以将文件名包含在 app.config 文件中并在您的代码中使用它们?

例如,此代码完美运行:

string folder = @"C:\CDR_FTP1\ttfiles_exported\";

如果我想从 app.config 文件中获取上述值

  1. 可以这样做吗?
  2. 语法是什么?

例如,在 app.config 中,我可以尝试使用以下内容:

<add key="InputDir" value=@"D:\CDR_FTP1\ttfiles_exported\" />

我无法添加“@”,因为它会给我以下错误:

"The character '@', hexadecimla value 0x40 is illegal at the beginning of an XML name."

如果我尝试在下面的代码中使用它,那很好,但是我需要在哪里以及如何粘贴“@”以使应用程序正确读取它?

string folder = ConfigurationSettings.AppSettings["InputDir"];
4

2 回答 2

9

只需使用:

<add key="InputDir" value="D:\CDR_FTP1\ttfiles_exported\" />

XML 属性中无需转义\

于 2013-01-08T22:14:44.803 回答
9

扩展基里尔的答案:

XML 属性中的特殊字符与 C# 字符串中的特殊字符不同。

In C# the character '\' is the string escape character, used to denote escape sequences such as '\n' for end of line, as you are probably aware; and hence needs to be escaped either as '\\' or by using @"".

In XML '\' has no especial meaning inside attribute values and can be use as is. This is not true for the '&' and '"' characters among others. If you wanted to include the value:

M & M "The chocolates"

in an app.config they would have to be escaped so:

<add key="SomeString" value="M &amp; M &quot;The chocolates&quot;" />
于 2013-01-08T22:26:49.417 回答