window setting.settings 詳解。

來源:互聯網
上載者:User

摘自:http://edu.admin5.com/article/20110124/0124431N2011.shtml

 

 

C# 應用程式設定 來源: 字型:[大 中 小]

C# 應用程式設定

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

使用VS內建的應用程式設定功能

  • 建立項目
  • 選擇菜單 [項目] > [屬性] 
  • 選擇 [設定]

就可手動添加應用程式設定了。

添加成功後,系統會自動產生App.config檔案。

view sourceprint?
01 <?xml version="1.0" encoding="utf-8"?>
02 <configuration>
03     <userSettings>
04         <WindowsApplication5.Properties.Settings>
05             <setting name="mySet" serializeAs="String">
06                 <value>testSet_828</value>
07             </setting>
08             <setting name="FormTitle" serializeAs="String">
09                 <value>FormTestdddd</value>
10             </setting>
11         </WindowsApplication5.Properties.Settings>
12     </userSettings>
13 </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則程式將取到新值。很奇怪,微軟為什麼要弄的怎麼複雜。

在程式中使用

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

總結

  • 應該是如果選擇範圍是User時,此設定是使用者層級的。使用不同使用者登入後運行程式取的值是不一樣的。
  • 並且如果程式修改了名稱,各自會擁有另一套App.config。
  • 而Application是應用程式級的,任何開啟相同程式的Application都會一樣。

自訂App.config

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

建立工程

手動添加App.config

格式如下:

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

引用 System.Configuration


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

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

使用方法

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.