C# 應用程序設置 Settings文件


C# 應用程序設置

官方參考:http://msdn.microsoft.com/zh-cn/library/k4s6c3a0(v=VS.80).aspx

使用VS自帶的應用程序設置功能

  • 創建項目
  • 選擇菜單 [項目] > [屬性] 
  • 選擇 [設置]

就可手動添加應用程序設置了。

添加成功后,系統會自動生成App.config文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
<? xml  version="1.0" encoding="utf-8"?>
< configuration >
     < userSettings >
         < WindowsApplication5.Properties.Settings >
             < setting  name="mySet" serializeAs="String">
                 < value >testSet_828</ value >
             </ setting >
             < setting  name="FormTitle" serializeAs="String">
                 < value >FormTestdddd</ value >
             </ setting >
         </ WindowsApplication5.Properties.Settings >
     </ userSettings >
</ configuration >

關於User和Application的區別

  • Application 不允許在程序中更新設置。只能手動更改App.config或到項目屬性的設置中更改。
  • User 允許在程序中更改設置。

VS也提供了一種直接在窗體控件屬性的ApplicationSettings 里設置關聯應用程序的快捷方法。

以下列舉了使用VS自帶應用程序應注意的地方

  • 如果范圍是Application時,在程序此值時只讀的。只能通過修改App.config的對應項來更改。
  • 如果范圍是User,並且在程序未對此值做修改時,修改App.config對應項,在程序訪問時當前值為App.config中設置的值。
  • 如果范圍是User,並且在程序中對此值進行了修改,App.config中記錄的還會是老值,並且以后的App.config此項設置將無效。

那到底User修改后的值系統在什么地方存這呢?

經過測試是在C:\Documents and Settings\Administrator\Local Settings\Application Data\Phook\WindowsApplication5.exe_Url_nlwmvagksxwiigfpn5ymssyrjtyn22ph\1.0.0.0\user.config

下存着,如果更改以上App.config則程序將取到新值。很奇怪,微軟為什么要弄的怎么復雜。

在程序中使用

1
2
3
4
5
6
//獲取值
label1.Text = Properties.Settings.Default.mySet;
label2.Text = Properties.Settings.Default.myApp;
//修改值,只能修改User范圍的。
Properties.Settings.Default.mySet = "test1111" ;
Properties.Settings.Default.Save();

總結

  • 應該是如果選擇范圍是User時,此設置是用戶級別的。使用不同用戶登錄后運行程序取的值是不一樣的。
  • 並且如果程序修改了名稱,各自會擁有另一套App.config。
  • 而Application是應用程序級的,任何打開相同程序的Application都會一樣。

自定義App.config

可能你想要一個Config可以功能和Application范圍一樣,但有同時支持程序修改。以下是實現方法

創建工程

手動添加App.config

格式如下:

1
2
3
4
5
6
<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
     < appSettings >
         < add  key="y" value="this is Y"/>     
     </ appSettings >
</ configuration >

引用 System.Configuration

把對App.config的操作合並成了一個類方便調用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Configuration;
 
//注意需要引用 System.Configuration
 
public  class  AppConfig
{
     private  static  Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 
     /// <summary>
     /// 獲取
     /// </summary>
     /// <param name="key"></param>
     /// <returns></returns>
     public  static  string  GetValue( string  key)
     {
         string  strReturn = null ;
         if  (config.AppSettings.Settings[key] != null )
         {
             strReturn = config.AppSettings.Settings[key].Value;
         }
         return  strReturn;
     }
 
     /// <summary>
     /// 設置
     /// </summary>
     /// <param name="key"></param>
     /// <param name="value"></param>
     public  static  void  SetValue( string  key, string  value)
     {
         if  (config.AppSettings.Settings[key] != null )
         {
             config.AppSettings.Settings[key].Value = value;
         }
         else
         {
             config.AppSettings.Settings.Add(key, value);
         }
         config.Save(ConfigurationSaveMode.Modified);
     }
 
     /// <summary>
     /// 刪除
     /// </summary>
     /// <param name="key"></param>
     public  static  void  DelValue( string  key)
     {
         config.AppSettings.Settings.Remove(key);
     }
}

使用方法

1
2
3
4
//修改或添加
AppConfig.SetValue( "dtNow" , DateTime.Now.Millisecond.ToString());
//獲取
label1.Text = AppConfig.GetValue( "dtNow" );

示例代碼下載:http://files.cnblogs.com/zjfree/AppConfig.rar


免責聲明!

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



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