多個檔案共用一個App.config
app.config檔案的內容
有app.config檔案的使用方式(正常)
沒有app.config檔案的使用方式(引用別人的)
本文範例
Github Source Code下載
<configuration>
<connectionstrings>
<add connectionstring="XXXX" name="Test4" providername="System.Data.SqlClient"/>
<appsettings>
<add key="Test1" value="這是組態檔第一筆資料"/>
<add key="Test2" value="這是組態檔第二筆資料"/>
<add key="Test3" value="這是組態檔第三筆資料"/>
</appsettings>
</configuration>
有app.config檔案的使用方式(正常)
private void Form1_Load(object sender, EventArgs e)
{
//直接讀取
label1.Text = ConfigurationManager.ConnectionStrings["Test4"].ToString();
label2.Text = ConfigurationManager.AppSettings["Test2"].ToString();
label3.Text = ConfigurationManager.AppSettings["Test3"].ToString();
}
沒有app.config檔案的使用方式(引用別人的)
private void Form1_Load(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.StartupPath + "\\需要App組態檔.exe");
//讀取ConnectionStrings內資料
label1.Text = config.ConnectionStrings.ConnectionStrings["Test4"].ToString();
//讀取AppSettings內資料
label2.Text = config.AppSettings.Settings["Test2"].Value;
label3.Text = config.AppSettings.Settings["Test3"].Value;
}
本文範例
Github Source Code下載
留言
張貼留言
您好,我是 Lawrence,這裡是我的開發筆記的網誌,如果你對我的文章有任何疑問或者有錯誤的話,歡迎留言讓我知道。