VS2005中提供的一個方便的功能就是[專案]中->[Properties]->[Settings.settings],一個方常方便的功能,提供了User層級及Application層級的應用程式設定值設定.
可以透過VS2005的功能修改及讀取,甚至是Reset為原始值.(下兩圖所示)


但是有時候不想要讓使用者動到Application層級的設定值,或是讓人家知道程式的重要設定值,你可以進行加密的動作,像是下圖
以下是一個簡單的範例Step by Step教你達成...
(1)建立你的應用程式及設定你的Application Setting

(2)在Form1建構式中加入這行
this.Text = MyDemo.Properties.Settings.Default.MyApplicationName;
下圖所示

(3)新增一個安裝程式類別 如下圖


(4)於安裝程式類別 override Install Method & Add ProtectSection Function
using System.Configuration;
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
//get Protected Configuration Provider name from custom action parameter
string sectionName = this.Context.Parameters["sectionName"];
//get Protected Configuration Provider name from custom action parameter
string provName = this.Context.Parameters["provName"];
// get the exe path from the default context parameters
string exeFilePath = this.Context.Parameters["assemblypath"];
//encrypt the configuration section
ProtectSection(sectionName, provName, exeFilePath);
}
private void ProtectSection(string sectionName, string provName, string exeFilePath)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(exeFilePath);
ConfigurationSection section = config.GetSection(sectionName);
if (!section.SectionInformation.IsProtected)
{
//Protecting the specified section with the specified provider
section.SectionInformation.ProtectSection(provName);
}
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
(5)打開app.config 並加入下面Section
<configProtectedData>
<providers>
<add useMachineProtection="true" name="DPAPIProtection"
type="System.Configuration.DpapiProtectedConfigurationProvider,System.Configuration,
 Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</configProtectedData>
(6)新增一安裝專案(加密部分需安裝後才會看的出來)
(7)修改安裝專案內容



修改屬性內容如下
/sectionName="applicationSettings/MyDemo.Properties.Settings" /provName="DPAPIProtection"
(8) Add Ref System.Configuration
(9) 編譯程式 (如果有按上面步驟操作應該會編譯成功)

(10)安裝應用程式

安裝完後,大功告成...
確認結果

範例程式[MD5 : c77f290441698787fc94d3325a2b41b4]
Source Code[MD5 : 4339ec62e8bfed90c3d6efa177152456]