[C#.NET][VB.NET] 應用程式組態設定檔 / Application Configurable File (二)
學習完本篇將學會
- 瞭解組態檔(*exe.config)的四大結構用法:<userSettings>跟<applicationSettings>、<appSttings>和<connectionStrings>,至於該如何靈活應用就在於個人了。
- 如何修改Application Scope的資料,學習處理XML檔案的好機會。
- 如何使用反射來處理設定檔
上篇[C#.NET][VB.NET] 應用程式組態設定檔 / Application Configurable File (一),很久以前的記錄,昨天發現仍有不足之處,於是再補充一下。
app.Config檔案的<userSettings>跟<applicationSettings>這兩個結構初始狀態會跟著exe.config跑,只要對<userSettings>結構有修改的動作它會將資料另外存到:
WinXP
C:\Documents and Settings\使用者\Local Settings\Application Data
Win7
C:\Users\使用者\AppData\Local
接下來我們來演練一下看看檔案是不是真的存下去了。
觀察組態檔userSettings、applicationSettings結構
1.建立一個專案
2.編輯Settings.settings檔,User、Application Scope這兩種權限,User(userSettings)是可供讀寫,Application(applicationSettings)只能讀
PS.上圖狀態就是結構的初始狀態。
3.按F5後,觀察應用程式的組態檔PropertiesSetting.exe.config以及PropertiesSetting.vshost.exe.config;PropertiesSetting.exe.config是給編譯完成的執行檔用的而PropertiesSetting.vshost.exe.config是給Visual Studio執行時用的。
這兩個檔案的<userSettings>,<applicationSettings>結構長的會都一樣
4.寫資料進去組態檔的<userSettings>結構看看變化如何
{
Properties.Settings setting = Properties.Settings.Default;
setting.myName = textBox4.Text;
setting.Save();
}
結果發現應用程式資料夾內組態檔跟本沒變化,但是確實是從程式內可以讀到阿,別急!原來是檔案是跑到C:\Users\9GY\AppData\Local\Microsoft底下了。資料夾一樣有分編譯完成用還有VS執行時用。
PS.不同版本的VS可能會有不一樣的資料夾,這部份沒有做太多的測試基本上我都是到C:\Users\使用者\AppData\Local底下的資料夾撈撈看。
<userSettings>結構修改的部份獨立出來跑來這裡了
如何修改applicationSettings結構
既然已經知道<applicationSettings>結構是跟著應用程式的config跑,檔案路徑不會亂變,那我們就執行針對檔案結構做修改
加入以下程式
{
string path = Application.ExecutablePath + ".config";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
if (File.Exists(path))
{
doc.Load(path);
string configString = @"configuration/applicationSettings/WindowsFormsApplication1.Properties.Settings/setting[@name='myAge']/value";
System.Xml.XmlNode configNode = doc.SelectSingleNode(configString);
if (configNode != null)
{
configNode.InnerText = this.textBox5.Text;
doc.Save(path);
Properties.Settings.Default.Reload();
}
}
}
比較麻煩的是結構的部份,密密麻麻的可別抄錯了
各位看倌,這是簡單型別的處理,若是複雜型別該怎麼處理呢?嘿嘿……(需要好多處理步驟,期待有人寫心得)
設定檔(*.settings)可以有很多個嗎?
當然可以呀!!!
如果你檔案想要擺在Properties資料夾裡,可以用Ctrl+C,Ctrl+V的方式
修改檔名並新增以下內容
app.Confog也立即幫我們產生customSettings的結構
如果不想要擺在Properties資料夾裡,也可以自己新增,記得要按資料夾分類好,才不會太亂喔。
程式用法當然也一樣。
利用反射列舉結構
{
this.listBox1.Items.Clear();
ApplicationSettingsBase app = (ApplicationSettingsBase)Assembly.Load("PropertiesSetting").CreateInstance("PropertiesSetting.Properties."+this.comboBox1.Text);
string str = "";
foreach (SettingsProperty item in app.Properties)
{
str = item.Name + "," + item.DefaultValue + Environment.NewLine;
listBox1.Items.Add(str);
}
}
觀察組態檔appSettings、connectionStrings結構
看完了<userSettings>跟<applicationSettings>結構後
請加入System.Configuration.dll參考。
接下來再來看看<appSttings>和<connectionStrings>結構,這兩個結構一定都是跟著應用程式組態檔跑,所以可以擺一些東西在裡面,如果要對這兩個結構加密請看[C#.NET][VB.NET] 保護 應用程式 組態設定 / Protect Application Configurable。
<appSttings>區段中開發者可以針對應用程式設定,將其設定存放至<appSttings>區段,它是以key/value名稱存放。<connectionStrings>是用name/connectionString方式存放。
操作<appSttings>區段,我們需要使用ConfigurationManager類別和AppSettingsSection類別
ConfigurationManager類別主要是用來決定要開啟什麼等級的組態檔。
AppSettingsSection類別是用來取得結構內的狀態
詳細範例如下
{
//打開用戶端的config
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//appSettings結構
string sectionName = config.AppSettings.SectionInformation.Name;
AppSettingsSection section = (AppSettingsSection)config.GetSection(sectionName);
//判斷KEY是否存在
string[] keys = section.Settings.AllKeys;
bool IsExist = false;
foreach (var item in keys)
{
if (item==textBox6.Text)
{
IsExist = true;
break;
}
}
if (IsExist)
config.AppSettings.Settings.Remove(textBox6.Text);//若不移除value會一直累加
config.AppSettings.Settings.Add(textBox6.Text, textBox7.Text + "," + config.AppSettings.SectionInformation.Name);
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection(sectionName);
}
操作<connectionStrings>區段,我們需要使用ConfigurationManager類別和ConnectionStringsSection類別以及ConfigurationSettings 類別
{
//打開用戶端的config
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//appSettings結構
string sectionName = config.ConnectionStrings.SectionInformation.Name;
ConnectionStringsSection section = config.ConnectionStrings;
//建立連線字串
ConnectionStringSettings csSettings = new ConnectionStringSettings(textBox8.Text, textBox9.Text);
//也可以不用判斷是否存在就下達移除指令
section.ConnectionStrings.Remove(csSettings);
section.ConnectionStrings.Add(csSettings);
config.Save(ConfigurationSaveMode.Full);
}
程式完成編譯後。
執行PropertiesSetting.exe然後觀察PropertiesSetting.exe.config檔案的變化
範例下載
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET