uses Registry;procedure RegisterFile; const EXT = '.txt'; begin with TRegistry.Create do begin RootKey := HKEY_CLASSES_ROOT; if OpenKey(EXT, True) then begin WriteString(Copy(EXT, 2, Length(EXT) + 'file'); CloseKey; end; if OpenKey(Copy(EXT, 2, Length(EXT) + 'file', True) then begin WriteString('', Application.ExeName + ' %1'); CloseKey; end;
Free; end; end;
procedure TForm1.FormCreate(Sender: TObject); begin RegistryFile;
if FileExists(ParamStr(1)) then Memo1.Lines.LoadFromFile(ParamStr(1)); end;
procedure registerfiletype(ft,key,desc,icon,prg:string); //ft:文件名后缀,如可以为'.tst'或'afile.tst' //key:在注册表中的键值,要唯一 //desc:关联程序的描述 //icon:缺省的程序图标,如Application.ExeName+',1',可以缺省 //prg:对应的应用程序 var myreg : treginifile; ct : integer; begin // 取文件的后缀 ct := pos('.',ft); while ct > 0 do begin delete(ft,ct,1); ct := pos('.',ft); end; if (ft = '') or (prg = '') then exit; //判断后缀及应用程序是否有效 ft := '.'+ft; myreg := treginifile.create(''); try myreg.rootkey := hkey_classes_root; // 根应该为HKEY_CLASSES_ROOT if key = '' then key := copy(ft,2,maxint)+'_auto_file'; // 如果没给出键值,则自动创建一个 myreg.writestring(ft,'',key); // 写入描述的键值 myreg.writestring(key,'',desc); // 写入描述 if icon <> '' then myreg.writestring(key+'\DefaultIcon','',icon); // 写入缺省图标 myreg.writestring(key+'\shell\open\command','',prg+' "%1"'); //写入相关联的应用程序 finally myreg.free; end; end; 调用例子: Example: registerFileType('.tst', 'testfile', '测试关联文件', Application.ExeName+',1', Application.ExeName); 原理说明: 程序关联的键值放在注册表根为HKEY_CLASSES_ROOT下面。实现文件关联要写入两 个键值:后缀键值和描述键值(可用术语用的不对) 如实现将.tst与程序tstfile.exe相关联 其中后缀键值形为: .tst myprogram key值为文件后缀,取值为对于key的一个描述 描述键值形为: myprogram 文件描述 其中还有相应的子键,如缺省的程序图标,形为: myprogram\DefaultIcon '', 'tstfile.exe,1' 打开命令,形为: myprogram\shell\open\command '', 'tstfile.exe "%1"' 写入完这两个键值后就可以实现文件与应用程序相关联了。
Registry;procedure RegisterFile;
const
EXT = '.txt';
begin
with TRegistry.Create do
begin
RootKey := HKEY_CLASSES_ROOT; if OpenKey(EXT, True) then
begin
WriteString(Copy(EXT, 2, Length(EXT) + 'file');
CloseKey;
end; if OpenKey(Copy(EXT, 2, Length(EXT) + 'file', True) then
begin
WriteString('', Application.ExeName + ' %1');
CloseKey;
end;
Free;
end;
end;
begin
RegistryFile;
if FileExists(ParamStr(1)) then
Memo1.Lines.LoadFromFile(ParamStr(1));
end;
//ft:文件名后缀,如可以为'.tst'或'afile.tst'
//key:在注册表中的键值,要唯一
//desc:关联程序的描述
//icon:缺省的程序图标,如Application.ExeName+',1',可以缺省
//prg:对应的应用程序
var myreg : treginifile;
ct : integer;
begin // 取文件的后缀
ct := pos('.',ft);
while ct > 0 do begin
delete(ft,ct,1);
ct := pos('.',ft);
end;
if (ft = '') or (prg = '') then exit; //判断后缀及应用程序是否有效
ft := '.'+ft;
myreg := treginifile.create('');
try
myreg.rootkey := hkey_classes_root; // 根应该为HKEY_CLASSES_ROOT
if key = '' then key := copy(ft,2,maxint)+'_auto_file';
// 如果没给出键值,则自动创建一个
myreg.writestring(ft,'',key); // 写入描述的键值
myreg.writestring(key,'',desc); // 写入描述
if icon <> '' then
myreg.writestring(key+'\DefaultIcon','',icon);
// 写入缺省图标
myreg.writestring(key+'\shell\open\command','',prg+' "%1"');
//写入相关联的应用程序
finally
myreg.free;
end;
end;
调用例子:
Example:
registerFileType('.tst',
'testfile',
'测试关联文件',
Application.ExeName+',1',
Application.ExeName);
原理说明:
程序关联的键值放在注册表根为HKEY_CLASSES_ROOT下面。实现文件关联要写入两
个键值:后缀键值和描述键值(可用术语用的不对)
如实现将.tst与程序tstfile.exe相关联
其中后缀键值形为: .tst myprogram
key值为文件后缀,取值为对于key的一个描述
描述键值形为: myprogram 文件描述
其中还有相应的子键,如缺省的程序图标,形为:
myprogram\DefaultIcon '', 'tstfile.exe,1'
打开命令,形为:
myprogram\shell\open\command '', 'tstfile.exe "%1"'
写入完这两个键值后就可以实现文件与应用程序相关联了。