Tag Archive for 'command'

Associate your program with file extension

article source : http://www.delphi3000.com/articles/article_525.asp?SK=
author : Igor Siticov

For creating file associations you should make some registry changes and inform Windows explorer about your changes.

For launching your program as default for all unregistered file types just associate it for “*” file type.

The following unit includes realization of function for creating file association.
See comments in source for details.

  1.  
  2. unit utils;
  3.  
  4. interface
  5. uses Registry, ShlObj, SysUtils, Windows;
  6.  
  7. procedure RegisterFileType(cMyExt, cMyFileType, cMyDescription, ExeName: string; IcoIndex: integer; DoUpdate: boolean = false);
  8.  
  9. implementation
  10.  
  11. procedure RegisterFileType(cMyExt, cMyFileType, cMyDescription, ExeName: string; IcoIndex: integer; DoUpdate: boolean = false);
  12. var
  13.    Reg: TRegistry;
  14. begin
  15.   Reg := TRegistry.Create;
  16.   try
  17.     Reg.RootKey := HKEY_CLASSES_ROOT;
  18.     Reg.OpenKey(cMyExt, True);
  19.     // Write my file type to it.
  20.     // This adds HKEY_CLASSES_ROOT\.abc\(Default) = ‘Project1.FileType’
  21.     Reg.WriteString(, cMyFileType);
  22.     Reg.CloseKey;
  23.     // Now create an association for that file type
  24.     Reg.OpenKey(cMyFileType, True);
  25.     // This adds HKEY_CLASSES_ROOT\Project1.FileType\(Default)
  26.     //   = ‘Project1 File’
  27.     // This is what you see in the file type description for
  28.     // the a file’s properties.
  29.     Reg.WriteString(, cMyDescription);
  30.     Reg.CloseKey;    // Now write the default icon for my file type
  31.     // This adds HKEY_CLASSES_ROOT\Project1.FileType\DefaultIcon
  32.     //  \(Default) = ‘Application Dir\Project1.exe,0′
  33.     Reg.OpenKey(cMyFileType + ‘\DefaultIcon’, True);
  34.     Reg.WriteString(, ExeName + ‘,’ + IntToStr(IcoIndex));
  35.     Reg.CloseKey;
  36.     // Now write the open action in explorer
  37.     Reg.OpenKey(cMyFileType + ‘\Shell\Open’, True);
  38.     Reg.WriteString(, ‘&Open’);
  39.     Reg.CloseKey;
  40.     // Write what application to open it with
  41.     // This adds HKEY_CLASSES_ROOT\Project1.FileType\Shell\Open\Command
  42.     //  (Default) = ‘"Application Dir\Project1.exe" "%1"’
  43.     // Your application must scan the command line parameters
  44.     // to see what file was passed to it.
  45.     Reg.OpenKey(cMyFileType + ‘\Shell\Open\Command’, True);
  46.     Reg.WriteString(, ‘"’ + ExeName + ‘" "%1"’);
  47.     Reg.CloseKey;
  48.     // Finally, we want the Windows Explorer to realize we added
  49.     // our file type by using the SHChangeNotify API.
  50.     if DoUpdate then SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  51.   finally
  52.     Reg.Free;
  53.   end;
  54. end;
  55.  
  56. end.
  57.