2012-01-16 86 views
5

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

+5

不要执行reg文件。直接使用注册表API。 – 2012-01-16 08:13:35

+0

我相信你给出的建议很有价值,但请你解释为什么我不应该这样做?还有一件事,我想要执行的注册表文件包含大量由数据感知网格组件自动插入的条目。如果我尝试手动编写它不会浪费时间吗? – 2012-01-16 08:24:50

+0

例如,你将如何处理64位系统上的注册表重定向器? – 2012-01-16 08:31:23

回答

12

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

+0

我该如何在Delphi中做到这一点?我仍然使用ShellExecute?一个例子,将不胜感激 – 2012-01-16 08:30:08

+4

ShellExecute会正常工作。我自己我会使用'reg import'而不是'regedit'。 – 2012-01-16 09:06:33

11

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

  1. %WINDIR%\ SYSTEM32 \ REGEDIT.EXE /s的 file.reg
  2. %WINDIR%\ SYSTEM32 \ REG.EXE进口file.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; 
相关问题