[C#.NET][VB.NET] 應用程式組態設定檔 / Application Configurable File (一)

  • 54497
  • 0
  • C#
  • 2010-08-08

[C#.NET][VB.NET] 應用程式組態設定檔 / Application Configurable File (一)

1.組態檔組態檔(exe.config)必須與應用程式(.exe)存在於同一層目錄,檔名必須與應用程式檔名相同+.config,如下圖所示:

快照-200941216427

 

2.組態檔是一種XML格式文件,裡面包含了應用程式的所需的配置設定,必免將程式的變數寫成死變數(hard-coding variable),別聽到XML別馬上傻傻的用之前提到的XmlSerializer 序列化,System.Configuration 命名空間ConfigurationManager 提供了讀寫組態檔的方法。

3.組態檔機器等級(Machine Level),位於"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config",所有的程式都會套用它的組態設定,修改時請先備份,以便改壞了還可以還原。

4.組態檔應用程式等級(Application Level),它與應用程式同一目錄。

5.VS預設未將System.Configuration 命名空間加入參考,所以開發者必須要手動加入,並且using System.Configuration。

6.ConfigurationManager 有兩個屬性分別是ConnectionStringsAppSettings;這兩個屬性分別對應到組態檔的<connectionStrings><appSettings>區段中。參考.NET Framework 的組態檔結構描述

快照-2009412172016

7.<appSttings>區段中開發者可以針對應用程式設定,將其設定存放至<appSttings>區段,它是以key/value方式存放。同理<connectionStrings>也是與<appSttings>有相同的功能,但<connectionStrings>是用name/connectionString方式存放。

快照-2009413143136

快照-2009413154355

8.應用程式的組態範圍分為兩個一是Application,另一個為User

Application是指該應用程式所有使用者的共享資訊,例如像是公司行號、電話、統編等;固定且不會依使用者改變的而改變。而Application範圍是唯讀的項目(有方法可寫,不過很麻煩),讀取的方式如下:

VB用My.Setting

C#用Properties.Setting.Default:
textBox7.Text = CS_AppConfig.Properties.Settings.Default.myName;

CS_AppConfig為專案的名稱,C#在建立組態時會在專案下產生一個Prooerties命名空間的Setting類別,用Default屬性就能取得範圍內容了。

兩者的語法存取有所不同

快照-20094131624

User是指應用程式登入使用者的個別組態,每個使用者可以擁有自己的設定,並且存放在使用者組態檔案中,它可供應用程式讀寫。

快照-200941316110

如何建立專案的設定組態

1.開啟專案的"屬性"

快照-2009413123520 快照-2009413171748

2.選取"設定"→依情況設定型別、範圍。

快照-200941312384

建立好之後就能在專案看到app.config的檔案以及內容

快照-2009413124935

 

C#完整範例

private void button1_Click(object sender, EventArgs e)
{
    //引用Configuration類別開啟組態
    Configuration cf1 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    this.textBox1.Text = cf1.FilePath;
    Configuration cf2 = ConfigurationManager.OpenMachineConfiguration();
    this.textBox2.Text = cf2.FilePath;
} 
 
private void button2_Click(object sender, EventArgs e)
{
    //讀取<appSettings>標籤
    ConfigurationManager.RefreshSection("appSettings");
    this.textBox3.Text = ConfigurationManager.AppSettings["myHome"];
    this.textBox4.Text = ConfigurationManager.AppSettings["myPhone"];
} 
 
private void button3_Click(object sender, EventArgs e)
{
    //利用專案.Properties讀取區段
    textBox5.Text = CS_AppConfig.Properties.Settings.Default.BlogURL;
    textBox6.Text = CS_AppConfig.Properties.Settings.Default.LoveColor.Name;
    textBox7.Text = CS_AppConfig.Properties.Settings.Default.myName;
} 
 
private void button4_Click(object sender, EventArgs e)
{
    //寫入User Scope
    CS_AppConfig.Properties.Settings.Default.myName = textBox8.Text;
} 
 
private void button5_Click(object sender, EventArgs e)
{
    //讀取<connectionStrings>區段
    textBox9.Text = ConfigurationManager.ConnectionStrings["AppCon.Properties.Settings.cnToNorthwind"].ConnectionString; 
 
}

 

 

 

 

 

 

VB完整範例

Imports System.Configuration
Public Class Form1
    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        '引用Configuration類別開啟組態 
        Dim cf1 As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Me.textBox1.Text = cf1.FilePath
        Dim cf2 As Configuration = ConfigurationManager.OpenMachineConfiguration()
        Me.textBox2.Text = cf2.FilePath
    End Sub 
 
    Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click
        '讀取<connectionStrings>區段 
        textBox9.Text = ConfigurationManager.ConnectionStrings("AppCon.Properties.Settings.cnToNorthwind").ConnectionString
    End Sub 
 
    Private Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button4.Click
        '寫入User Scope 
        My.Settings.myName = textBox8.Text
    End Sub 
 
    Private Sub button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        '讀取<appSettings>標籤 
        ConfigurationManager.RefreshSection("appSettings")
        Me.textBox3.Text = ConfigurationManager.AppSettings("myHome")
        Me.textBox4.Text = ConfigurationManager.AppSettings("myPhone")
    End Sub 
 
    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        '利用專案.Properties讀取區段 
        textBox5.Text = My.Settings.BlogURL
        textBox6.Text = My.Settings.LoveColor.Name
        textBox7.Text = My.Settings.myName
    End Sub
End Class

 

 

 

 

 

範例下載:
CS_AppConfig.rar
VB_AppConfig.rar

快照-200941316540

續:[C#.NET][VB.NET] 應用程式組態設定檔 / Application Configurable File (二)

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo