加入命名空間

   1: using Microsoft.Win32;

加入自行定義的MyToolkit函式庫

   1:  
   2: #region 寫入RegistryKey
   3: public static void writeRegistryKey(string path, string varname, string value)
   4: {
   5:         RegistryKey myReg1, myReg2;
   6:         myReg1 = Registry.CurrentUser;
   7:         myReg2 = myReg1.CreateSubKey(path);
   8:         myReg2.SetValue(varname, value);
   9: }
  10: #endregion
  11: #region 讀取RegistryKey
  12: public static string loadRegistryKey(string path, string varname)
  13: {
  14:         string value = "";
  15:         RegistryKey myReg1, myReg2;
  16:         myReg1 = Registry.CurrentUser;
  17:         myReg2 = myReg1.CreateSubKey(path);
  18:         try
  19:         {
  20:                value = myReg2.GetValue(varname).ToString();
  21:         }
  22:         catch { }
  23:         return value;
  24: }
  25: #endregion
  26:     }
  27: }

撰寫一個Windows Form測試, 專案名稱:StartFormByLClosePosition

image

加入參考: MyToolkit函式庫

image

<專案>選擇MyToolkit

image

讀取

   1: private void LoadRegSub()
   2:  
   3:    string path = "Software\\MySoft";
   4:    string varname = "";
   5:    string str;
   6:  
   7:    varname = "1";
   8:    str = MyToolkit.SystemTool.loadRegistryKey(path, varname);
   9:    textBox1.Text = str;
  10:    //----------------------------------------------------------------
  11:    varname = "2";
  12:    str = MyToolkit.SystemTool.loadRegistryKey(path, varname);
  13:    textBox2.Text = str;
  14:    // 設定Form位置
  15:    this.Location = new Point( Int16.Parse(textBox1.Text), Int16.Parse(textBox2.Text));

寫入

   1: private void writeRegSub()
   2: {
   3:     string path = "Software\\MySoft";
   4:     string varname = "";
   5:     string str;
   6:     // 寫入Form位置置註冊檔
   7:     varname = "1";
   8:     MyToolkit.SystemTool.writeRegistryKey(path, varname, this.Location.X.ToString());
   9:     varname = "2";
  10:     MyToolkit.SystemTool.writeRegistryKey(path, varname, this.Location.Y.ToString());
  11: }
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()