{changing the default application associated with a certain file type this simple example bumps notepad out of the way and makes this little program the default application for text files when a text file is clicked it will be displayed by this program in a memo clicking button two restores notepad (or whatever was there before as the default application for textfiles you will need two buttons and a memo and then after doing this paste this code in place of the default text for the unit (if you paste the code and then try to drop the buttons etc it won't work} you will also have to click the buttons to initialize the button click events. Also find the 'oncreate' event handler for the form on the properties menu and double click that to init the form create event code required if this is going to work the registry key for text files '.txt' contains a string that indicates a key name for the associated application here we will save the key name in a text file so we can restore notepad or some other program later when button 2 is clicked and then we will create a key for this program 'ATextApp' and after this we will insert the key name 'ATextApp' as the data for the 'txt' file extension key making this program the default for text files. after toying with this program you might want to choose 'run' from the start menu and then 'regedit' as the program name and then find the key 'ATextApp' and choose delete, just to keep your registry from being cluttered by one more useless key } unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Registry; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Memo1: TMemo; procedure CreateDefaultprogramRegistryKeys(AppName : string); procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; ApplicationName : string; TextFileName : string; implementation {$R *.DFM} procedure TForm1.CreateDefaultprogramRegistryKeys(AppName : string); var regkey : tregistry; shortname : array[0..512] of char; var f : textfile; AssocAppKeyName : string; found : boolean; begin found := false; {save the current app used to launch text files} RegKey := TRegistry.Create; try RegKey.RootKey := HKEY_CLASSES_ROOT; {false = don't create key} If RegKey.OpenKey('.txt', false) then begin found := true; AssocAppKeyName := RegKey.ReadString(''); {the empty quotes mean the first or 'default' key data entry} end; finally Regkey.free; end; if not found then begin showmessage('the text file key was not found'); exit; end; {save the key data to use to restore the text file association later} assignfile(f, extractfilepath(Applicationname) + 'txtapp.txt'); rewrite(f); writeln(f, AssocAppkeyName); closefile(f); {now create a key for this application so it can become the default application for text files} {get short 8.3 dos type filename for reg key applicationname is returned by Formcreate - always the first parameter of ParamStr shortname is a char buffer, and 512 is the length} GetShortPathName(Pchar(ApplicationName), shortname, 512); RegKey := TRegistry.Create; try RegKey.RootKey := HKEY_CLASSES_ROOT; {true = create key} If RegKey.Openkey('ATextApp\DefaultIcon', true) then begin {empty quotes means first or default value, turn the shortname char buffer into a string and then indicate that the default icon is number 0} RegKey.WriteString('', string(shortname) + ',0'); end; finally regkey.free; end; {create the shell path open command key data} RegKey := TRegistry.Create; try RegKey.RootKey := HKEY_CLASSES_ROOT; {true = create key} If RegKey.Openkey('ATextApp\Shell\Open\Command', true) then begin {the shell open command key contains the path to the application that will be used when a text file is clicked in explorer} RegKey.WriteString('', string(shortname) + ' "%1"'); end; finally regkey.free; end; {keys have been created for this demo program} {now make this program the default for text files by making its key the default value for the txt file extension key} Regkey := TRegistry.Create; try RegKey.RootKey := HKEY_CLASSES_ROOT; If RegKey.OpenKey('.txt', false) then begin RegKey.WriteString('', 'ATextApp'); end; finally RegKey.free; end; {now when a text file is clicked in explorer it will launch this application and the form create procedure will display the text file in the memo} end; procedure TForm1.FormCreate(Sender: TObject); var f : textfile; s : string; begin ApplicationName := ParamStr(0); {the first program parameter is always the application path and filename} If ParamCount = 1 then TextFileName := ParamStr(1) else TextfileName := ''; {the next parameters would include such things as the name of the textfile clicked in explorer or something a user typed in as a parameter on a command line} {the buttons must be clicked to init the click event handler} Button1.top := 8; Button1.width := 177; Button1.left := 16; Button2.top := 40; Button2.width := 177; Button2.left := 16; Button1.Caption := 'Change Text File Association'; Button2.caption := 'Restore Text File Association'; Memo1.top := 80; Memo1.left := 16; Memo1.width := 505; memo1.Height := 257; Memo1.lines.clear; {use the name of the textfile passed as a parameter to fill in the memo with the textfile} If FileExists(TextfileName) then begin assignfile(f, textfilename); reset(f); while not eof(f) do begin readln(f, s); Memo1.lines.append(s); end; closefile(f); end; end; {make this program the default program for textfiles} procedure TForm1.Button1Click(Sender: TObject); begin CreateDefaultprogramRegistryKeys(ApplicationName); end; {restore the previous default text file program} procedure TForm1.Button2Click(Sender: TObject); var f : textfile; s : string; regkey : TRegistry; begin If not FileExists(extractfilepath(Applicationname) + 'txtapp.txt') then exit; {read in the key name of the app previously associated with text files} assignfile(f, extractfilepath(Applicationname) + 'txtapp.txt'); reset(f); readln(f, s); closefile(f); {write the data back into the key to restore the previous association for text files} Regkey := TRegistry.Create; try RegKey.RootKey := HKEY_CLASSES_ROOT; If RegKey.OpenKey('.txt', false) then begin RegKey.WriteString('', s); end; finally RegKey.free; end; end; end.