XP 之后, Delphi 动注册表不方便了...逼出来一个办法:转万一博客
手头的程序需要修改注册表, 以让当前程序成为某格式的默认打开程序并关联图标; Vista 之后需要管理员权限才能操作注册表, 很麻烦,
所以有了下面的尝试.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
Memo1:
TMemo;
Button1:
TButton;
procedure
FormCreate(Sender: TObject);
procedure
Button1Click(Sender: TObject);
private
{ Private
declarations }
public
{ Public
declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses System.IOUtils, Winapi.ShellAPI, Winapi.ShlObj; //RegApp 函数用到的单元
//关联默认程序的函数
procedure RegApp(const aExt, aAppName, aAppPath: string);
const
nRegFmt = 'Windows Registry Editor Version
5.00'
+ sLineBreak +
'[HKEY_CLASSES_ROOT\%0:s]'
+ sLineBreak +
'@="%1:s"'
+ sLineBreak +
'[HKEY_CLASSES_ROOT\%1:s\DefaultIcon]'
+ sLineBreak +
'@="%2:s,0"'
+ sLineBreak +
'[HKEY_CLASSES_ROOT\%1:s\shell]'
+ sLineBreak +
'[HKEY_CLASSES_ROOT\%1:s\shell\open]'
+ sLineBreak +
'[HKEY_CLASSES_ROOT\%1:s\shell\open\command]' + sLineBreak +
'@="%3:s"';
var
RegStr: string;
str3, str4: string;
RegTmpFile: string;
begin
str3 := aAppPath.Replace('\', '\\');
str4 := Format('"%s" "%%1"',
[str3]).Replace('"', '\"');
RegStr := Format(nRegFmt, [aExt, aAppName, str3,
str4]);
RegTmpFile := TPath.GetTempPath +
'RegTmp.reg';
with TStringList.Create do begin
unit Unit1;
interface
uses
type
var
implementation
{$R *.dfm}
uses System.IOUtils, Winapi.ShellAPI, Winapi.ShlObj; //RegApp 函数用到的单元
//关联默认程序的函数
procedure RegApp(const aExt, aAppName, aAppPath: string);
const
var
begin