注册表设置例子

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
 }
}
 

在Windows操作系统中,注册表是一个重要的数据库,用于存储系统和应用程序的配置信息。注册表中的每个项(也称为“键”)包含一系列的值,这些值可以是不同的数据类型。例如,%1是一种常见的字符串替换操作,通常用于程序和脚本中,以代表传递给命令的第一个参数。 在批处理文件或命令行命令中使用%1,可以引用在命令行中输入的第一个参数。例如,如果你有一个批处理文件叫`example.bat`,在这个文件的第一行中写入`@echo %1`,然后在命令提示符下运行这个批处理文件并传递一个参数(比如`example.bat hello`),则`%1`会被替换成`hello`,并且`hello`这个字符串会被显示在命令提示符窗口中。 如果要设置注册表参数,通常使用reg命令。例如,要添加一个新的注册表项或修改一个已有的注册表项,可以使用如下命令: ``` reg add HKCU\Software\MyApp /v Version /d "1.0" /f ``` 这条命令会在当前用户的注册表根键`HKEY_CURRENT_USER`下,`Software`键下添加(或修改)一个名为`MyApp`的子键,并在该子键下创建一个名为`Version`的值,其数据类型为`REG_SZ`(字符串值),数据为`1.0`。参数`/f`表示强制覆盖,即使该键已存在。 需要注意的是,修改注册表是一个高风险操作,不当的修改可能会导致系统不稳定或应用程序运行异常。在进行注册表操作前,建议备份注册表,并确保了解操作的具体含义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值