【问题标题】:How to avoid confirmation message when executing a .reg file with shellexecute command使用 shellexecute 命令执行 .reg 文件时如何避免确认消息
【发布时间】:2012-01-16 08:10:42
【问题描述】:

在我的程序中,我在启动时检查注册表项,如果它不存在,我在 ShellExecute 命令的帮助下执行位于应用程序文件夹中的 reg 文件。执行此命令时如何避免收到确认消息。有没有办法做到这一点,或者出于安全原因这是不可能的?

【问题讨论】:

  • 不要执行 reg 文件。直接使用注册表 API。
  • 我相信您提供的建议很有价值,但您能否解释一下为什么我不应该这样做?还有一件事,我要执行的注册表文件包含大量由数据感知网格组件自动插入的条目。如果我尝试手动编写它是否意味着浪费时间?
  • 例如,您将如何处理 64 位系统上的注册表重定向器?
  • 我相信这个 reg 密钥不在 HKLM 中。否则无法导入。
  • 另外,您需要管理员权限才能在 UAC 环境中运行 Regedit。肯定会弹出 UAC 警告对话框,要求用户确认。

标签: delphi registry message shellexecute confirmation


【解决方案1】:

使用 /s 命令行开关。 (见http://support.microsoft.com/kb/82821

【讨论】:

  • 我如何在 Delphi 中做到这一点?我还使用 ShellExecute 吗?一个例子将不胜感激
  • ShellExecute 可以正常工作。我自己我会使用reg import 而不是regedit
  • 链接到 MS 已损坏,因为它曾经是
【解决方案2】:

这是可能的。两种方法是:

  1. %windir%\system32\regedit.exe /s file.reg
  2. %windir%\system32\reg.exe 导入文件.reg

要么将 file.reg 静默导入注册表。

【讨论】:

  • 这行得通,只是它现在提示您必须具有管理员权限,所以提示数量相同。
【解决方案3】:

试试这个导入 *.reg 文件,

  procedure ImportRegistry;
       var
        strProgram :String ;
        strCommand :String ;
        fileOne   :String ;
      begin

fileOne:=ExtractFilePath(Application.ExeName)+  'my_Resources\Default.reg';
strProgram := 'REGEDIT' ;
strProgram := strProgram + #0 ;
strCommand := '/SC /C ' + ExtractShortPathName(fileOne) ;
strCommand := strCommand + #0 ;

if ShellExecute(0,nil,@strProgram[1],@strCommand[1],nil,SW_HIDE) <= 32 then
  begin
        ShowMessage(SysErrorMessage(GetLastError)) ; //if there is any error in importing
  end;


end;

你也可以试试这个链接unitEXRegistry.pas

这个unitEXRegistry.pas单元有非常有用的功能,可以导出注册表文件,也可以静默导入导出的*.reg文件

       procedure exportREgis;
        var
         texpr : TExRegistry;
        begin
         texpr:=TExRegistry.Create;
         texpr.RootKey:=HKEY_CURRENT_USER;
         texpr.OpenKeyReadOnly('\MyKey');
         texpr.ExportKey (ExtractFilePath(Application.ExeName)+'ExportedReg.reg');
         texpr.Free; 
       end;

然后导入你可以使用(静默)

     procedure TForm1.Button1Click(Sender: TObject);
        var
         texpr : TExRegistry;
        begin
          texpr:=TExRegistry.Create;
          texpr.ImportRegFile('c:\myReg.reg');
          texpr.Free;
       end;

【讨论】:

    【解决方案4】:

    显然 REG IMPORT 中有一个错误 - 它将成功消息写入 STDERR 而不是 STDOUT。

    下面的 .bat 代码解决了这个问题。成功消息被丢弃,但显示失败消息。

    SET RegError=%TEMP%\RegError.txt
    REG IMPORT "%Settings.reg%" 2>"%RegError%" && DEL /Q "%RegError%" || @(ECHO Error importing %Settings.reg%: & TYPE "%RegError%" & PAUSE)
    SET RegError=
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      相关资源
      最近更新 更多