如何从应用程序处理文件关联? [英] How to handle File Associations from Application?
问题描述
无论如何,我希望能够直接从我的应用程序中的首选项表单设置/删除我的应用程序将使用的文件类型。
需要哪些方法来做到这一点,我正在考虑到注册表的路线,但是我们正在使用什么键/值等,如果注册表是方式去?
欣赏一些建议和提示,它也适用于XP / Vista / 7。
感谢Advance。
尝试此单元将某个扩展名与exe关联删除注册表项取消注册。
unit utils;
接口
使用注册表,ShlObj,SysUtils,Windows;
程序RegisterFileType(cMyExt,cMyFileType,cMyDescription,ExeName:string; IcoIndex:integer; DoUpdate:boolean = false);
实现
程序RegisterFileType(cMyExt,cMyFileType,cMyDescription,ExeName:string; IcoIndex:integer; DoUpdate:boolean = false);
var
注册:TRegistry;
begin
注册:= TRegistry.Create;
try
Reg.RootKey:= HKEY_CLASSES_ROOT;
Reg.OpenKey(cMyExt,True);
//将我的文件类型写入它。
//添加HKEY_CLASSES_ROOT\.abc\(Default)='Project1.FileType'
Reg.WriteString('',cMyFileType);
Reg.CloseKey;
//现在为该文件类型创建一个关联
Reg.OpenKey(cMyFileType,True);
//添加HKEY_CLASSES_ROOT\Project1.FileType\(默认)
// ='Project1文件'
//这是你在$ b $的文件类型描述中看到的b //文件的属性。
Reg.WriteString('',cMyDescription);
Reg.CloseKey; //现在为我的文件类型
写入默认图标//添加HKEY_CLASSES_ROOT\Project1.FileType\DefaultIcon
// \(Default)='Application Dir\Project1.exe,0 '
Reg.OpenKey(cMyFileType +'\DefaultIcon',True);
Reg.WriteString('',ExeName +','+ IntToStr(IcoIndex));
Reg.CloseKey;
//现在在explorer
中写入open操作Reg.OpenKey(cMyFileType +'\Shell\Open',True);
Reg.WriteString('','& Open');
Reg.CloseKey;
//使用
编写应用程序打开它//添加HKEY_CLASSES_ROOT\Project1.FileType\Shell\Open\Command
//(默认)='应用程序目录\Project1.exe%1'
//您的应用程序必须扫描命令行参数
//以查看传递给它的文件。
Reg.OpenKey(cMyFileType +'\Shell\Open\Command',True);
Reg.WriteString('','''+ ExeName +'%1');
Reg.CloseKey;
//最后,我们希望Windows资源管理器实现我们使用SHChangeNotify API添加
//我们的文件类型。
如果DoUpdate然后SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_IDLIST,nil,nil);
finally
Reg.Free;
结束
结束
结束。
注册表是违规的事情...
I know Installers can setup File Associations for your Application during the installation process, so if you have your own File types that open with your Application, it will be setup to do that, and also the associated File will have its own icon in Windows what you define.
Anyway, I would like to be able to Set/Remove the File types my Application will use, directly from the preferences form in my Application.
What methods are needed to do this, I am thinking along the lines of the Registry, but then what Keys/Values etc are we to work with, if Registry is the way to go?
Appreciate some advice and tips, It is also important that it works on XP/Vista/7.
Thanks in Advance.
try this unit to associate a certain extension to an exe remove the entries made in registry to unregister.
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.
Registry is defenetly the way to go with things...
这篇关于如何从应用程序处理文件关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!