Associating File Type

Peter Kiers
Peter Kiers used Ask the Experts™
on
Dear Experts,

I have found a code on this site to make a file have a DIFFERENT icon
than the icon of the .exe file.

This is the way to do it:

[1] Create a file in Notepad and write the following text

    X Icon FileIcon.ico  // this is the icon of your associated-file type

[2] Save the file as "Icon.rc"

[3] Make sure that the file "FileIcon.ico" are in the same folder as "Icon.rc" and then open the command prompt (Start -> Run -> type "cmd") and type (without the quotes) "brcc32 YOUR_PATH\Icon.rc". This will produce a file called "Icon.res".

[4] In Delpi, open your project and add the following line

{ .. code .. }

implementation

{$R *.dfm}
{$R Icon.res}  // add this line

{ .. code ..}

[5] The code:

procedure TForm1.RegisterFileType;
var
Reg : TRegistry;
Begin
    Reg := TRegistry.Create;
   try
    with Reg do
   begin
    RootKey := HKEY_CLASSES_ROOT;
    OpenKey('\Project1', True);
    WriteString('', 'Project1 File');
CloseKey;
  OpenKey('Project1\DefaultIcon', True);
  WriteString('', Application.ExeName + ',1');
CloseKey;
OpenKey('Project1\shell\open\command', True);
   WriteString('', Application.ExeName + ' %1');
CloseKey;
   RootKey := HKEY_CLASSES_ROOT;
   OpenKey('\.pkd', True);
  WriteString('', 'Project1');
  CloseKey;
  end;
finally
    Reg.CloseKey;
    Reg.Free;
end;
end;

BUT this procedure above works only on WindowsXP!!!!
On Windows Vista I get an error.

I allready asked this question in the Vista-zone. They told me to run
the application as Administrator. But didn't help either.
I hope someone can help me in the Delphi-zone.

Greetings, Peter Kiers



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Can't you do this from Windows Explorer - Folder Options - File Types instead of programmatically?
Peter KiersOperator

Author

Commented:
What I want is to make a file have a DIFFERENT icon
than the icon of the .exe file. And the procedure above works great on XP
but not on Vista.

P.
Try this

unit Unit1;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Registry, ShlObj,
   StdCtrls, ExtCtrls;

type
   TForm1 = class (TForm)
     Panel1: TPanel;
     StaticText1: TStaticText;
     StaticText2: TStaticText;
     StaticText3: TStaticText;
     StaticText4: TStaticText;
     Edit1: TEdit;
     Edit2: TEdit;
     Edit3: TEdit;
     Edit4: TEdit;
     Edit5: TEdit;
     StaticText5: TStaticText;
     CheckBox1: TCheckBox;
     Button1: TButton;
     procedure Button1Click (Sender: TObject);
     procedure FormCreate (Sender: TObject);
   private
     (Private declarations)
   public
     (Public declarations)
   end;

var
   Form1: TForm1;

implementation

($ R *. DFM)
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 custom file suffix
     Reg.WriteString ('', cMyFileType);
     Reg.CloseKey;
     / / Write custom file types
     / / Format: HKEY_CLASSES_ROOT \ cMyExt \ (Default) = 'cMyFileType'

     / / The following link for the file type to create
     Reg.OpenKey (cMyFileType, True);
     Reg.WriteString ('', cMyDescription);
     / / Write to file a description of the type of information
     Reg.CloseKey;

     / / The following file types to choose a custom icon
     / / Adding button format HKEY_CLASSES_ROOT \ cMyFileType \ DefaultIcon
     / / \ (Default) = 'Application Dir \ Project1.exe, 0'
     Reg.OpenKey (cMyFileType + '\ DefaultIcon', True);
     Reg.WriteString ('', ExeName + ',' + IntToStr (IcoIndex));
     Reg.CloseKey;

     / / The following registration in the Explorer to open documents
     Reg.OpenKey (cMyFileType + '\ Shell \ Open', True);
     Reg.WriteString ('', '& Open');
     Reg.CloseKey;

     / / Format: HKEY_CLASSES_ROOT \ Project1.FileType \ Shell \ Open \ Command
     / / (Default) = ' "Application Dir \ Project1.exe" "% 1"'
     Reg.OpenKey (cMyFileType + '\ Shell \ Open \ Command', True);
     Reg.WriteString ('', ' "' + ExeName + '" "% 1"');
     Reg.CloseKey;

     / / Finally, let the realization of our Explorer's file types to join, just call your SHChangeNotify
     if DoUpdate then SHChangeNotify (SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
   finally
     Reg.Free;
   end;
end;

procedure TForm1.FormCreate (Sender: TObject);
begin
   Edit1.Text: = '. Hell';
   Edit2.Text: = 'Project1.hell';
   Edit3.Text: = 'hellins''s file type';
   Edit4.Text: = Application.Exename;
   Edit5.Text: ='0 ';
   checkbox1.checked: = true;
end;

procedure TForm1.Button1Click (Sender: TObject);
begin
   RegisterFileType (Edit1.text, Edit2.Text, Edit3.Text, Edit4.Text, strtoint (Edit5.Text), Checkbox1.Checked);
end;

end.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Peter KiersOperator

Author

Commented:
What to put in the edit-field in the OnCreate-event. My data is:

application: ITKnowledgebase.exe
file type extention: .pkd
file icon: FileIcon.ico
app icon: AppIcon.ico
procedure TForm1.FormCreate (Sender: TObject);
begin
   Edit1.Text: = '.pkd; //your file type extension
   Edit2.Text: = '.pkd'; // your file type
   Edit3.Text: = 'pkd files'; //description
   Edit4.Text: = ITKnowledgebase; //exename
   Edit5.Text: ='0 '; //icon index
   checkbox1.checked: = true;
end;
Peter KiersOperator

Author

Commented:
Thanks
Peter Kiers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial