註冊表設置例子

using System;
using Microsoft.Win32;

namespace RegistryProc
{
 /// <summary>
 /// RegistryProc
 /// </summary>
 public class RegistryProc
 {
  #region parameter define
  private const string strAutoKey = @"HKEY_CURRENT_USER";
  private const string strSubKey = @"/Software/Microsoft/Windows/CurrentVersion/Run";
  private const char chrSeparator = '//';
  #endregion

  #region
  /// <summary>
  /// RegistryProc
  /// </summary>
  public RegistryProc()
  {
   SetRegKey("myexe","c://text.exe",strSubKey,Registry.LocalMachine);
   DeleteRegKey("myexe",strSubKey,Registry.LocalMachine);
  }
  #endregion

  #region SetRegKey
  /// <summary>
  /// Set Registry Key
  /// </summary>
  /// <param name="strKeyName"></param>
  /// <param name="strFilePath"></param>
  /// <param name="strSubKey"></param>
  /// <param name="RegKey"></param>
  /// <returns></returns>
  private bool SetRegKey(string strKeyName,string strFilePath,string strSubKey,RegistryKey RegKey)
  {
   try
   {
    RegistryKey runKey=GetRunKey(strSubKey,RegKey);
    runKey.SetValue(strKeyName,strFilePath);
    runKey.Close();
   }
   catch
   {
    return false;
   }
   return true;
  }
  #endregion

  #region DeleteRegKey
  /// <summary>
  /// Delete Registry Key
  /// </summary>
  /// <param name="strKeyName"></param>
  /// <param name="strSubKey"></param>
  /// <param name="RegKey"></param>
  /// <returns></returns>
  private bool DeleteRegKey(string strKeyName,string strSubKey,RegistryKey RegKey)
  {
   try
   {
    RegistryKey runKey=GetRunKey(strSubKey,RegKey);
    //circle in Values belong to key
    foreach (string strTemp in runKey.GetValueNames())
    {
     //find the key want to be delete
     if (strTemp == strKeyName)
     {
      runKey.DeleteValue(strTemp);
      break;
     }
    }
    runKey.Close();
   }
   catch
   {
    return false;
   }
   return true;
  }
  #endregion

  #region GetRunKey
  /// <summary>
  /// get the last sub key with recursion
  /// </summary>
  /// <param name="strKeyTree">subkey tree</param>
  /// <param name="rgkParent">Parent registrykey</param>
  /// <returns></returns>
  private RegistryKey GetRunKey(string strKeyTree,RegistryKey rgkParent)
  {
   RegistryKey rgkChild;
   string strChild = string.Empty;
   string strRemain = string.Empty;

   //has one / char, or not
   if(strKeyTree .IndexOf (chrSeparator )!=strKeyTree .LastIndexOf (chrSeparator ))
   {
    //remove / in the head
    strKeyTree = strKeyTree.Substring(1, strKeyTree.Length - 1);
    //get first sub key name
    strChild=strKeyTree .Substring (0,strKeyTree.IndexOf (chrSeparator ));
    //get the first sub key
    rgkChild = rgkParent.OpenSubKey(strChild);
    //keep the remain part of strKeyTree except first part
    strRemain = strKeyTree.Substring(strKeyTree.IndexOf(chrSeparator), strKeyTree.Length - strKeyTree.IndexOf(chrSeparator));

    return GetRunKey(strRemain, rgkChild);

   }
   else //only the last sub tree key
   {
    strKeyTree = strKeyTree.Substring(1, strKeyTree.Length - 1);
    rgkChild = rgkParent.OpenSubKey(strKeyTree,true);
    return rgkChild;
   }
  }
  #endregion
 }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章