我知道安装程序可以在安装过程中为您的应用程序设置文件关联,所以如果您有自己的文件类型可以随您的应用程序打开,它将被设置为执行此操作,并且关联的文件将在 Windows 中有自己的图标你定义。
无论如何,我希望能够直接从我的应用程序的首选项表单中设置/删除我的应用程序将使用的文件类型。
需要什么方法来做到这一点,我正在按照注册表的思路思考,但是如果注册表是要走的路,那么我们要使用哪些键/值等?
欣赏一些建议和技巧,它适用于 XP/Vista/7 也很重要。
提前致谢。
我知道安装程序可以在安装过程中为您的应用程序设置文件关联,所以如果您有自己的文件类型可以随您的应用程序打开,它将被设置为执行此操作,并且关联的文件将在 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.
注册表无疑是处理事情的方式......
从您的应用程序中,您最好使用每用户存储进行文件关联。如果您使用系统范围的注册表位置,那么您需要提升才能应用更改。这不是您在标准用户应用程序中应该做的事情。
将注册表设置存储在:
HKEY_CURRENT_USER\SOFTWARE\Classes
那里的条目格式和下面的完全一样
HKEY_LOCAL_MACHINE\SOFTWARE\Classes
您可以从 shell http://support.microsoft.com/kb/184082运行以下命令
或者您可以在注册表中输入如下链接 http://www.daycounter.com/LabBook/Changing-File-Associations-With-Registry-Editor.phtml