使用ConfigurationManager類讀寫配置文件


使用ConfigurationManager類 讀寫配置文件app.config,以下為代碼:

using System;   
using System.Configuration;   
  
static class Program   
    {   
        static void Main()   
        {   
            showConfig();   
            UpdateAppSettings();   
            showConfig();   
  
            Console.ReadKey(true);   
        }   
  
        private static void showConfig()   
        {   
            string  = ConfigurationManager.AppSettings["Directory"];   
            Console.WriteLine("AppSetting配置節 Path key的value為:" + dir + "\n");   
        }   
  
        /// <summary>   
        /// UpdateAppSettings    
        /// </summary>   
        public static void UpdateAppSettings()   
        {   
            // Get the configuration file.    
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);   
            Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");   
            config.AppSettings.Settings["Directory"].Value = "tset";   
  
            // Save the configuration file.    
            config.AppSettings.SectionInformation.ForceSave = true;   
            config.Save(ConfigurationSaveMode.Modified);   
            // Force a reload of the changed section.    
            ConfigurationManager.RefreshSection("appSettings");   
        }



  
using System;   using System.Configuration;   static class Program { static void Main() { showConfig(); UpdateAppSettings(); showConfig(); Console.ReadKey(true); } private static void showConfig() { string = ConfigurationManager.AppSettings["Directory"]; Console.WriteLine("AppSetting配置節 Path key的value為:" + dir + "\n"); } /// <summary> /// UpdateAppSettings /// </summary> public static void UpdateAppSettings() { // Get the configuration file. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("config.FIlePath: " + config.FilePath + "\n"); config.AppSettings.Settings["Directory"].Value = "tset"; // Save the configuration file. config.AppSettings.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. ConfigurationManager.RefreshSection("appSettings"); }

app.config內容:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
  <appSettings>  
    <add key="Directory" value="C:\Documents and Settings"/>  
  </appSettings>  
</configuration>  
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Directory" value="C:\Documents and Settings"/>
  </appSettings>
</configuration>

代碼結果:app.config只能作為初始化的定義,工程生成后運行程序集名稱.exe 修改生成后的 程序集名稱.exe.Config文件

一開始調試時看到控制結果是想要的結果,但看app.config配置文件內容沒變(vs2008 F5調試模式下是修改 程序集名稱.vshost.exe.config配置文件)還以為是代碼有問題,網上搜,也有人碰過到此現像,原來是自己沒有理解到MSDN的說明。(還是有文化差異啊)如:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");

查看config.filePath值,即了解明白了。

4.0的類庫: http://msdn.microsoft.com/en-us/library/ms134265(v=VS.100).aspx

注意:此類是.net2.0后新增。

必須要先在工程里添加system.configuration.dll程序集的引用。

(在解決方案管理器中右鍵點擊工程名稱,在右鍵菜單中選擇添加引用,.net->組件名稱->下即可找到)

添加引用后可以使用System.Configuration空間下的ConfigurationManager類.

 

引用資源:

Read/Write App.Config File with .NET 2.0

在程序里修改配置文件

C#讀寫app.config中的數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM