C#基本知識 -- App.config檔案的配置與讀取
阿新 • • 發佈:2019-02-13
對於需要配置的常量,在App.config中進行配置,格式為:
<add key="變數名" value="變數值">如:
- <?xmlversion="1.0"encoding="utf-8"?>
- <configuration>
- <appSettings>
- <addkey="SourceFolder"value="D:\CodeFolder\MysolutionTest"/>
- <addkey="FindingString"value="要搜尋的字串"/>
-
<addkey="OutputFile"
- </appSettings>
- </configuration>
在主程式中,需要讀取App.config檔案中配置的變數,採用
NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
來獲取各個變數名和變數值。如
NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
string path = appSettings["SourceFolder"];在主程式中,需要讀取App.config檔案中配置的變數,採用
NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
來獲取各個變數名和變數值。如
- NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
- string path = appSettings["SourceFolder"];