开发者

App.config application settings and variables

开发者 https://www.devze.com 2023-04-01 08:29 出处:网络
I think this will be a simple question.I have an App.config that contains a path <appSettings> <add key=\"Path\" value=\"C:\\\\Test\\\\\" />

I think this will be a simple question. I have an App.config that contains a path

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

I want to add a couple more paths to the App.config so I don't have them hard coded in my C# windows service. Will it work if I change this

string newPath = @"C:\SecondTest\" + fileName;

to this

string newPath = ConfigurationManager.AppSettings["SecondPa开发者_运维技巧th"] + fileName;

I could then create SecondPath in the App.config.


Yes, it will work but it would be better to combine paths like this instead of using string concatenations:

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


Yes that would work (did you try it ?).


AppSettings works like key-value storage so it will work however you like

0

精彩评论

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