C# WinForm中 App.config 檔案配置

來源:互聯網
上載者:User

標籤:

應用程式設定檔,對於asp.net是 web.config對於WINFORM程式是 App.Config(ExeName.exe.config)。

 

設定檔,對於程式本身來說,就是基礎和依據,其本質是一個xml檔案,對於設定檔的操作,從.NET 2.0 開始,就非常方便了,提供了 System [.Web] .Configuration 這個管理功能的NameSpace,要使用它,需要添加對 System.configuration.dll的引用。

 

對於WINFORM程式,使用 System.Configuration.ConfigurationManager;

對於ASP.NET 程式, 使用 System.Web.Configuration.WebConfigurationManager;

 

我們以最常見的 AppSettings 小節來作為例子:

假設有如下的設定檔內容:

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="y" value="this is Y"/>

  </appSettings>

</configuration>

 

1. 讀取值:

 

Asp.Net:   

System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];

 

WinForm: 

System.Configuration.ConfigurationManager.AppSettings[“y”];

 

2. 添加一項

 

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

Configuration config = WebConfigurationManager.OpenWebConfiguration(null); 

AppSettingsSection app = config.AppSettings; 

app.Settings.Add("x", "this is X"); 

config.Save(ConfigurationSaveMode.Modified);

 

WinForm:

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

AppSettingsSection app = config.AppSettings; 

app.Settings.Add("x", "this is X"); 

config.Save(ConfigurationSaveMode.Modified);

 

3. 修改一項

 

Asp.Net

Configuration config = WebConfigurationManager.OpenWebConfiguration(null); 

AppSettingsSection app = config.AppSettings; //app.Settings.Add("x", "this is X"); 

app.Settings["x"].Value = "this is not Y"; 

config.Save(ConfigurationSaveMode.Modified);

 

WinForm

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

AppSettingsSection app = config.AppSettings; //app.Settings.Add("x", "this is X"); 

app.Settings["x"].Value = "this is not Y"; 

config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");// 重新整理命名節,在下次檢索它時將從磁碟重新讀取它。記住應用程式要重新整理節點

 

【修改後,App.config檔案的x節點沒有更改,而是exe.config的配置更改,讀取正常】

 

4. 刪除一項

 

Asp.Net

Configuration config = WebConfigurationManager.OpenWebConfiguration(null); 

AppSettingsSection app = config.AppSettings;

app.Settings.Remove("x");

config.Save(ConfigurationSaveMode.Modified);

 

WinForm

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

AppSettingsSection app = config.AppSettings; 

app.Settings.Remove("x"); 

config.Save(ConfigurationSaveMode.Modified);

 

C# WinForm中 App.config 檔案配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.