Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically Associate Files? 1

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
GB
hi, im writing a program which allows me to store code snippets, i can save and load these fine, the file type for saved/loaded files are *.codelib

ive added a second .ico file in a res file created with BRCC32, no problems, using Res Hacker i can see this is the second icon in the exe.

Ive been looking through the registry to see how some files are associated, its a bit confusing with registry keys all over, so instead i double clicked the .codelib file from windows, and selected my program to open it. thats fine, it loads my program (and i can handle the file opened from windows with ParamCount). the problem is the icon it gives is the same as my program, only on a white background.

the registry key for my app looks like this:

Code:
Key										Type			Value

HKEY_CLASSES_ROOT\.codelib							String			codelib_auto_file
HKEY_CURRENT_USER\Software\Classes\.codelib					String			codelib_auto_file
HKEY_CURRENT_USER\Software\Classes\codelib_auto_file\shell\open\command		String			"D:\Programs\Code Library\CodeLibrary.exe" "%1"
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.CODELIB

if i change the %1 to %2 (which i assume would be the second icon in my exe) nothing happens.

excuse the longish post, but can anyone help guide me how to associate my *.codelib file with my program, whether that be with code, or the correct registry paths i need to add to. i know how to deal with the registry, thats not a problem, knowing what keys and values i need to add is.

Thank you all!
 
%1 in what you posted is a first parameter, meaning what you are passing to your program. %2 becomes the second parm that your program receives.

What you need to do to make this work is to code your program to accept parameters (and check them for validity). Look at ParamCount and ParamStr to do this.


This is what I found when I registered .testfileextension to open with Notepad (along with other things, I seem to think are related to other things). It seems like what you copied is reasonably on track. You just need to rig up what you have to accept a command-line parm.

If you're looking for an icon change, you'll probably have to reboot to see it.

Code:
HKEY_CLASSES_ROOT\.testfileextension
HKEY_CLASSES_ROOT\testfileextension_auto_file\shell\edit\command
HKEY_CLASSES_ROOT\testfileextension_auto_file\shell\open\command

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.testfileextension\OpenWithList
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.testfileextension\OpenWithProgids

Measurement is not management.
 
ah i see, well as mentioned i know how to handle the files opened from windows, my main concern was finding all the paths to what i need to enter for the file association.

i will look into the paths you posted.

Thanks
 
this is how i deal with the paramcount:

Code:
procedure TfrmMain.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  {delete any old traces of data first}
  FlushMainFolder;
  FlushTempFolder;

  CheckLibraryDirectoryIntegrity;

  DatabaseHasChanged:= False;

  MainTreeParent:= '';

  Self.WindowState:= wsMaximized;

  {did the user try and open the file from windows?}
  try
    if ParamCount = 0 then {opened normally}
    begin
      DatabaseName:= 'Not a file';
    end

    else

    if ParamCount = 1 then {opened from windows}
    begin
      for i:= 1 to ParamCount do
      begin
        frmMain.Show;

        DatabaseName:= ExtractFilePath(ParamStr(i)) + ExtractFileName(ParamStr(i));
        dlgOpen.FileName:= DatabaseName;

        SenderWasLoadDatabase:= True;
        SenderWasSaveDatabase:= False;
        SenderWasMoveSnippets:= False;

        LoadDatabase;

        Application.ProcessMessages;
      end;
    end;
  except on exception do
    Self.Refresh;
  end;
end;

now will try the file association....
 
I don't have any help for you on your problem, but I noticed this bit of code just above:
Code:
  except on exception do
    Self.Refresh;
  end;

which seems a bit needless. This code:
Code:
  except
    Self.Refresh;
  end;

is exactly the same. Or you could use something like this:
Code:
  except 
    on E: Exception do
    begin
      Application.MessageBox(PChar('Exception occurred: ' + E.Message), 'Uh oh', MB_OK);
      Self.Refresh;
    end;
  end;

By declaring E as a reference to the exception, you can access properties of the exception, such as the message.
 
thanks, i got into the habit of using [except on exception do], just goes to show theres multiple ways of doing things, no matter how simple :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top