文件的应用程序关联储存在Windows的注册表中。要获取此信息我们先要找回文件扩展名所在处的“Class”。这些信息可在此获得:
HKEY_CLASSES_ROOT\.ext\(default)
这里的".ext"就是你想找的文件的扩展名(如".txt", ".bmp"等)。接着我们可获取打开该类文件的命令行,可在此下获得有关数据:
HKEY_CLASSES_ROOT\class\Shell\Open\Command\(default)
"class"是按扩展名分类的文件分类,该字符串常以以下格式书写
"D:\PATH\APPNAME.EXT" "%1" -OPTIONS
%1即表示用指定应用程序打开文档,所以我们应该找到此字符串的位置并改成我们想替换成的文件名。
例子:
-------
这个函数将返回打开某个文档关联的命令行:
function GetAssociation(const DocFileName: string): string;
var
FileClass: string;
Reg: TRegistry;
begin
Result := '';
Reg := TRegistry.Create(KEY_EXECUTE);
Reg.RootKey := HKEY_CLASSES_ROOT;
FileClass := '';
if Reg.OpenKeyReadOnly(ExtractFileExt(DocFileName)) then
begin
FileClass := Reg.ReadString('');
Reg.CloseKey;
end;
if FileClass <> '' then begin
if Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') then
begin
Result := Reg.ReadString('');
Reg.CloseKey;
end;
end;
Reg.Free;
end;
转载于:https://www.cnblogs.com/myamanda/articles/1597569.html