开发者

How to handle File Associations from Application?

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...


From your app you'd be better to use the per-user store for file associations. If you use the system wide registry location then you'd need to elevate in order to apply changes. That's not something you should do in a standard user app.

Store the registry settings under:

HKEY_CURRENT_USER\SOFTWARE\Classes

The format of entries under there is exactly the same as under

HKEY_LOCAL_MACHINE\SOFTWARE\Classes


You can run the following command from your shell http://support.microsoft.com/kb/184082

or you can make the entry in registry as shown in the following link http://www.daycounter.com/LabBook/Changing-File-Associations-With-Registry-Editor.phtml

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜