You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Delphi中利用TIniFile将TRegIniFile注册表数据导出为INI文件?

把TRegIniFile对应的注册表数据导出为INI文件

刚好做过类似的需求,其实核心思路就是遍历注册表中的所有子项和键值,然后对应写入INI文件——毕竟TRegIniFile和TIniFile的访问模型本来就很像:注册表子项对应INI的Section,键值对对应Section下的Key=Value。

下面是完整的Delphi实现代码,包含了根路径下的无Section键值和所有子项的导出:

procedure ExportRegToIni(const RegPath, IniFilePath: string);
var
  RegIni: TRegIniFile;
  IniFile: TIniFile;
  Sections: TStringList;
  Keys: TStringList;
  I, J: Integer;
  StrValue: string;
  IntValue: Integer;
  BoolValue: Boolean;
begin
  // 初始化注册表访问实例,默认操作HKCU(当前用户)分支
  RegIni := TRegIniFile.Create(RegPath);
  try
    // 初始化INI文件实例
    IniFile := TIniFile.Create(IniFilePath);
    try
      // 第一步:导出根路径下的键值(对应INI中无Section的条目)
      Keys := TStringList.Create;
      try
        RegIni.GetKeyNames('', Keys); // 获取注册表根路径下的所有键名
        for J := 0 to Keys.Count - 1 do
        begin
          // 按常见类型依次读取(字符串→整数→布尔),可根据实际需求调整
          StrValue := RegIni.ReadString('', Keys[J], '');
          if StrValue <> '' then
            IniFile.WriteString('', Keys[J], StrValue)
          else
          begin
            IntValue := RegIni.ReadInteger('', Keys[J], -1);
            if IntValue <> -1 then
              IniFile.WriteInteger('', Keys[J], IntValue)
            else
            begin
              BoolValue := RegIni.ReadBool('', Keys[J], False);
              IniFile.WriteBool('', Keys[J], BoolValue);
            end;
          end;
        end;
      finally
        Keys.Free;
      end;

      // 第二步:导出所有子项(对应INI中的Sections)
      Sections := TStringList.Create;
      try
        RegIni.GetSectionNames(Sections); // 获取所有注册表子项
        for I := 0 to Sections.Count - 1 do
        begin
          Keys := TStringList.Create;
          try
            RegIni.GetKeyNames(Sections[I], Keys); // 获取当前子项的所有键名
            for J := 0 to Keys.Count - 1 do
            begin
              // 同样处理不同数据类型
              StrValue := RegIni.ReadString(Sections[I], Keys[J], '');
              if StrValue <> '' then
                IniFile.WriteString(Sections[I], Keys[J], StrValue)
              else
              begin
                IntValue := RegIni.ReadInteger(Sections[I], Keys[J], -1);
                if IntValue <> -1 then
                  IniFile.WriteInteger(Sections[I], Keys[J], IntValue)
                else
                begin
                  BoolValue := RegIni.ReadBool(Sections[I], Keys[J], False);
                  IniFile.WriteBool(Sections[I], Keys[J], BoolValue);
                end;
              end;
            end;
          finally
            Keys.Free;
          end;
        end;

        // 强制将缓存写入磁盘,避免数据丢失
        IniFile.UpdateFile;
      finally
        Sections.Free;
      end;
    finally
      IniFile.Free;
    end;
  finally
    RegIni.Free;
  end;
end;

关键细节说明

  1. 注册表分支选择:默认TRegIniFile.Create(RegPath)操作HKEY_CURRENT_USER(当前用户)分支,如果要操作HKEY_LOCAL_MACHINE,需要修改创建参数:

    RegIni := TRegIniFile.Create(RegPath, HKEY_LOCAL_MACHINE);
    

    注意:操作HKLM需要管理员权限,否则可能读取失败。

  2. 数据类型扩展:示例只处理了三种常见类型,如果你的注册表有二进制数据,建议改用TRegistry类直接访问,读取后转成Base64字符串写入INI。

  3. 使用示例:

    // 导出当前用户下的\Software\MyApp到D盘的配置文件
    ExportRegToIni('\Software\MyApp', 'D:\MyApp_Config.ini');
    

内容的提问来源于stack exchange,提问作者Xel Naga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:53:20