Solved

Associating File Types

Posted on 2009-07-12
14
323 Views
Last Modified: 2012-05-07
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 TMainForm.RegisterFileType;
var
Reg : TRegistry;
Begin
    Reg := TRegistry.Create;
   try
    with Reg do
   begin
    RootKey := HKEY_CLASSES_ROOT;
    OpenKey('\MyApp', True);
    WriteString('', 'MyApp File');
CloseKey;
  OpenKey('MyApp\DefaultIcon', True);
  WriteString('', Application.ExeName + ',0');
CloseKey;
OpenKey('MyApp\shell\open\command', True);
   WriteString('', Application.ExeName + '1');
CloseKey;
   RootKey := HKEY_CLASSES_ROOT;
   OpenKey('\.ext', True);
  WriteString('', 'MyApp');
  CloseKey;
  end;
finally
    Reg.CloseKey;
    Reg.Free;
end;
end;

When I fill in the RegisterFileType procedure, that i have put in the code-section i get:
Project ITKnowledgeBase.exe raised exception class ERegistryException with message 'Failed to set data for"'.

Here is my data:
My applicationname: IT Knowledgebase
Executable.name: ITKnowledgebase.exe
Icon name for the app: AppIcon.ico
Icon name for the file: FileIcon.ico
Filename: Icon.res
extension name:  .pkd


Greetings,

Peter Kiers
procedure TMainForm.RegisterFileType;
var
Reg : TRegistry;
Begin
    Reg := TRegistry.Create;
   try
    with Reg do
   begin
    RootKey := HKEY_CLASSES_ROOT;
    OpenKey('\ITKnowledgeBase', True);
    WriteString('', 'ITKnowledgeBase File');
CloseKey;
  OpenKey('ITKnowledgeBase\DefaultIcon', True);
  WriteString('', Application.ExeName + ',0');
CloseKey;
OpenKey('ITKnowledgeBase\shell\open\command', True);
   WriteString('', Application.ExeName + '1');
CloseKey;
   RootKey := HKEY_CLASSES_ROOT;
   OpenKey('\.PKD', True);
  WriteString('', 'ITKnowledgeBase');
  CloseKey;
  end;
finally
    Reg.CloseKey;
    Reg.Free;
end;
end;

Open in new window

0
Comment
Question by:peterkiers
  • 7
  • 5
  • 2
14 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 24835754
proposal from my side:

you can do all the things using inno setup compiler, no need for doing that stuff with selfwriten code ...

just google for delohi & inno seto up, it is a excellent free tool
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24840816
If you still do not want to do such using Inno, (an outstanding installer, by the way), here is a link to a delphi.about.com article on how to do it easily.

http://delphi.about.com/od/adptips2006/qt/appextension.htm

John
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24841247
I prefer the procedure above.

Greetings, Peter Kiers
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 8

Expert Comment

by:BdLm
ID: 24841665
is your issue solved, what is the latest status?
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24841740
Nothing is solved yet.

I have asked this question before a long time ago.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22010656.html

But now i am working on a new application, but i don't get the RegisterFileType-procedure right!
The file that is associated with my application doesn't get the fileicon.ico.

Peter
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24842208
Are you trying to do this under Vista?
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24842234
YES, sorry forgot to tell.
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24842297
You will need to run the application as an administrator OR ensure that the executable name has the word 'setup' or install' inside the executable name.

Try that. Right click your executable you created and runas an administrator and let us know what happens.

Also, did you download the code from delphi.about.com from the link I provided above? There are a couple registry settings and one create in delphi.about that you do not have in your code. I know their code works, so... might behoove you to look at it and I pasted it below.


procedure RegisterFileType(ExtName:String; AppName:String) ;
var
  reg:TRegistry;
begin
  reg := TRegistry.Create;
  try
   reg.RootKey:=HKEY_CLASSES_ROOT;
   reg.OpenKey('.' + ExtName, True) ;
   reg.WriteString('', ExtName + 'file') ;
   reg.CloseKey;
   reg.CreateKey(ExtName + 'file') ;
   reg.OpenKey(ExtName + 'file\DefaultIcon', True) ;
   reg.WriteString('', AppName + ',0') ;
   reg.CloseKey;
   reg.OpenKey(ExtName + 'file\shell\open\command', True) ;
   reg.WriteString('',AppName+' "%1"') ;
   reg.CloseKey;
  finally
   reg.Free;
  end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24842494
These are my data:

Here is my data:
My applicationname: IT Knowledgebase
Executable.name: ITKnowledgebase.exe
Icon name for the app: AppIcon.ico
Icon name for the file: FileIcon.ico
Filename: Icon.res
extension name:  .pkd

How do i fill in the RegistryFileType procedure?


0
 
LVL 1

Author Comment

by:peterkiers
ID: 24842505
I'll be back tommorrow something came up.
Johnjces the 500 p's are yours anyway.

Peter
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24842561
I have to ask at this point, what is the file extension you are wanting to use to open up some file in your application?

.klb or .know or .jct as examples. Something must be defined there as to what kind of an icon will we double click to open up this file with your application?

Or are you just trying to create a shortcut to open your executable. I am somewhat confused... which by the way is pretty normal!

John
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24846317
.pkd is the file extention.
0
 
LVL 18

Accepted Solution

by:
Johnjces earned 500 total points
ID: 24846529
Using the procedure supplied, try:

RegisterFileType('pkd', 'ITKnowledgebase.exe') ;

Probably have to run the app asAdministrator for it to work right.

John
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24848537
It works thanks. PK
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use Hashing sha1 in Delphi 2010 4 260
Delphi XE10, MySQL Query 4 143
Firemonkey webbrowser scrollbars ? 1 38
How to Get Images From Server using App Tethering 11 20
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question