2013年9月11日 星期三

[轉貼] 設定和取得登錄檔機碼(Registry Key)的值

出處:http://gogorice.blogspot.tw/2009/02/c-registry-key.html

設定機碼(Registry Key)
string keyPathString = "SOFTWARE\\AB Tool";
Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey programName = start.CreateSubKey(keyPathString);
programName.SetValue("Application Path", System.Windows.Forms.Application.StartupPath);
programName.Close();
執行的結果

-------------------------------------------------------------------------
取得機碼(Registry Key) 的值
string pathString = null;
string keyPathString = "SOFTWARE\\AB Tool";
Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey programName = start.OpenSubKey(keyPathString);
if (programName != null)
{
pathString = (string)programName.GetValue("Application Path");
}

====================================================

MSDN說明  http://msdn.microsoft.com/zh-tw/library/h5e7chcf.aspx

安全性

 更為安全的做法是將資料寫入至使用者資料夾 (Microsoft.Win32.Registry.CurrentUser),而不是寫入至本機電腦 (Microsoft.Win32.Registry.LocalMachine)。
當您建立登錄值時,必須先確定該值是否已經存在。 其他處理序 (也許是惡意的處理序) 可能已建立該值並且具有其存取權。 當您將資料放入登錄值時,其他處理序就可以使用該資料。 若要預防這個問題,請使用 Overload:Microsoft.Win32.RegistryKey.GetValue 方法。 如果機碼不存在,這個方法會傳回 null。
雖然登錄機碼受到存取控制清單 (ACL) 保護,但在登錄中以純文字方式存放機密資料 (例如密碼) 仍然是不安全的做法。

沒有留言:

張貼留言