gpt4 book ai didi

delphi - 如何处理应用程序中的文件关联?

转载 作者:行者123 更新时间:2023-12-03 15:48:53 25 4
gpt4 key购买 nike

我知道安装程序可以在安装过程中为您的应用程序设置文件关联,因此,如果您有自己的文件类型与您的应用程序一起打开,它将被设置为执行此操作,并且关联的文件将有自己的图标在 Windows 中您定义什么。

无论如何,我希望能够直接从我的应用程序中的首选项表单中设置/删除我的应用程序将使用的文件类型。

需要什么方法来做到这一点,我正在沿着注册表的思路思考,但是如果注册表是可行的方法,那么我们要使用什么键/值等?

感谢一些建议和提示,它在 XP/Vista/7 上运行也很重要。

提前致谢。

最佳答案

尝试此单元将某个扩展名与 exe 关联,删除注册表中的条目以取消注册。

unit utils; 

interface
uses Registry, ShlObj, SysUtils, Windows;

procedure RegisterFileType(cMyExt, cMyFileType, cMyDescription, ExeName: string; IcoIndex: integer; DoUpdate: boolean = false);

implementation

procedure RegisterFileType(cMyExt, cMyFileType, cMyDescription, ExeName: string; IcoIndex: integer; DoUpdate: boolean = false);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CLASSES_ROOT;
Reg.OpenKey(cMyExt, True);
// Write my file type to it.
// This adds HKEY_CLASSES_ROOT\.abc\(Default) = 'Project1.FileType'
Reg.WriteString('', cMyFileType);
Reg.CloseKey;
// Now create an association for that file type
Reg.OpenKey(cMyFileType, True);
// This adds HKEY_CLASSES_ROOT\Project1.FileType\(Default)
// = 'Project1 File'
// This is what you see in the file type description for
// the a file's properties.
Reg.WriteString('', cMyDescription);
Reg.CloseKey; // Now write the default icon for my file type
// This adds HKEY_CLASSES_ROOT\Project1.FileType\DefaultIcon
// \(Default) = 'Application Dir\Project1.exe,0'
Reg.OpenKey(cMyFileType + '\DefaultIcon', True);
Reg.WriteString('', ExeName + ',' + IntToStr(IcoIndex));
Reg.CloseKey;
// Now write the open action in explorer
Reg.OpenKey(cMyFileType + '\Shell\Open', True);
Reg.WriteString('', '&Open');
Reg.CloseKey;
// Write what application to open it with
// This adds HKEY_CLASSES_ROOT\Project1.FileType\Shell\Open\Command
// (Default) = '"Application Dir\Project1.exe" "%1"'
// Your application must scan the command line parameters
// to see what file was passed to it.
Reg.OpenKey(cMyFileType + '\Shell\Open\Command', True);
Reg.WriteString('', '"' + ExeName + '" "%1"');
Reg.CloseKey;
// Finally, we want the Windows Explorer to realize we added
// our file type by using the SHChangeNotify API.
if DoUpdate then SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
finally
Reg.Free;
end;
end;

end.

注册表无疑是解决问题的方法......

关于delphi - 如何处理应用程序中的文件关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com