[ASP.NET][C#]app.configweb.config的增加、修改、刪除操作

應用程式組態檔,對於asp.netweb.config,對於WindowsForm程式則是App.config(執行檔名稱.exe.config)。組態檔對於程式本身來說,就是基礎和依據,其本質是一個xml檔案,對於組態檔的操作,從.net2.0開始就方常的方便了,提供了System.Web.ConfigurationSystem.Configuration兩個命名空間,要使用它,需要加入參考。

WindowsForm中使用System.Configuration.ConfigurationManager

ASP.NET中使用System.Web.Configuration.WebConfigurationManager

對於組態檔內容的讀取,實在是太方便了XD

實作:

加入組態檔

以一小段App.config的內容來實作:

組態檔的內容如下:

view source

print?

1

2

 

3

4

"This is A value" key="A">

 

5

6

 

7

1.讀取值:

Asp.NetSystem.Web.Configuration.WebConfigurationManager.AppSettings["A"];

WinFormSystem.Configuration.ConfigurationManager.AppSettings["A"];

2.增加

ASP.NET(需要有寫入權限)

view source

print?

1

//ConfigurationAppSettingsSection必須引用System.Configuration才可使用!

2

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

 

3

AppSettingsSection app = config.AppSettings;

4

app.Settings.Add("B", "This is B value");

 

5

config.Save(ConfigurationSaveMode.Modified);

WinForm

view source

print?

1

//ConfigurationAppSettingsSection必須引用System.Configuration才可使用!

2

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 

3

AppSettingsSection app = config.AppSettings;

4

app.Settings.Add("B", "This is B value");

 

5

config.Save(ConfigurationSaveMode.Modified);

3.修改

ASP.NET(需要有寫入權限)

view source

print?

1

//ConfigurationAppSettingsSection必須引用System.Configuration才可使用!

2

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

 

3

AppSettingsSection app = config.AppSettings;

4

//app.Settings.Add("B", "This is B value");

 

5

app.Settings["A"].Value = "This is not B";

6

config.Save(ConfigurationSaveMode.Modified);

WinForm

view source

print?

1

//ConfigurationAppSettingsSection必須引用System.Configuration才可使用!

2

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 

3

AppSettingsSection app = config.AppSettings;

4

//app.Settings.Add("B", "This is B value");

 

5

app.Settings["A"].Value = "This is not B";

6

config.Save(ConfigurationSaveMode.Modified);

4.刪除

ASP.NET(需要有寫入權限)

view source

print?

1

//ConfigurationAppSettingsSection必須引用System.Configuration才可使用!

2

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

 

3

AppSettingsSection app = config.AppSettings;

4

//app.Settings.Add("B", "This is B value");

 

5

//app.Settings["A"].Value = "This is not B";

6

app.Settings.Remove("A");

 

7

config.Save(ConfigurationSaveMode.Modified);

WinForm

view source

print?

1

//ConfigurationAppSettingsSection必須引用System.Configuration才可使用!

2

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 

3

AppSettingsSection app = config.AppSettings;

4

//app.Settings.Add("B", "This is B value");

 

5

//app.Settings["A"].Value = "This is not B";

6

app.Settings.Remove("A");

 

7

config.Save(ConfigurationSaveMode.Modified);

以上就是對組態檔的增加/修改/刪除

另外也可以將連線資料庫的連線字串寫在組態檔內方便修改

如下:

組態檔內容:

view source

print?

1

2

 

3

"connDB" providername="System.Data.OleDb" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\soft.mdb;Persist Security Info=True">

4

 

5

6

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\soft.mdb;Persist Security Info=True" key="connDB2">

 

7

8

讀取:

view source

print?

1

ConfigurationManager.ConnectionStrings["connDB"].ConnectionString;

2

//讀取nameconnDB的連線字串

 

文章標籤
全站熱搜
創作者介紹
創作者 hsiung03 的頭像
hsiung03

hsiung.博格 ERP軟體

hsiung03 發表在 痞客邦 留言(0) 人氣(258)