App.config应用程序设置和变量

时间:2011-08-30 16:51:25

标签: c# web-config app-config

我认为这将是一个简单的问题。我有一个包含路径的App.config

<appSettings>
  <add key="Path" value="C:\\Test\\" />
</appSettings>

我想在App.config中添加更多路径,因此我没有在C#windows服务中对它们进行硬编码。如果我改变这个

它会起作用吗?
string newPath = @"C:\SecondTest\" + fileName;

到这个

string newPath = ConfigurationManager.AppSettings["SecondPath"] + fileName;

然后我可以在App.config中创建SecondPath。

3 个答案:

答案 0 :(得分:2)

是的,它会起作用,但最好是这样combine paths而不是使用字符串连接:

string newPath = Path.Combine(
    ConfigurationManager.AppSettings["SecondPath"], 
    fileName
);

答案 1 :(得分:0)

是的,这会奏效(你试过吗?)。

答案 2 :(得分:0)

AppSettings的工作方式类似于键值存储,因此无论您喜欢什么

相关问题