DELPHI AREA
MESSAGE BOARD
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

File Associations: Saving, setting, restoring.

 
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming
View previous topic :: View next topic  
Author Message
Johnny_Bit
Junior Member


Joined: 15 Jun 2003
Posts: 82

PostPosted: 06/09/03 19:04    Post subject: File Associations: Saving, setting, restoring. Reply with quote

Like in subject. how to do it?

Normally i would analyse keys in registry, but in this conditions it's allmost imposible to me. I want to register extension as good as does it Delphi instaler (ofcoz with DDE), but also save older association, and when user decides to unassociate extension it should restore saved association
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 208

PostPosted: 06/09/03 20:32    Post subject: Reply with quote

http://jrsoftware.org/isfaq.php#assoc

Hope that it helps.

Kambiz
Back to top
View user's profile Send e-mail Visit poster's website
Johnny_Bit
Junior Member


Joined: 15 Jun 2003
Posts: 82

PostPosted: 07/09/03 12:48    Post subject: Reply with quote

Well i want to do it without instalator. It should be in program.
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 208

PostPosted: 07/09/03 15:09    Post subject: Reply with quote

Although that link is related to an installer, however you just need to follow the registry links described on the page in your own program.
Back to top
View user's profile Send e-mail Visit poster's website
Johnny_Bit
Junior Member


Joined: 15 Jun 2003
Posts: 82

PostPosted: 08/09/03 15:54    Post subject: Reply with quote

Yes, you're right... but how to save/restore associations?
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 208

PostPosted: 08/09/03 18:07    Post subject: Reply with quote

GetAssociation reads the association information of the given file extension from registry.
SetAssociation assigns the association information of the given file extension to registry.
The file extension must begin with a perid.

Code:
uses
  Registry;

function GetAssociation(const Ext: String; var Name, Desc, Command, Icon: String): Boolean;
var
  R: TRegistry;
begin
  Result := False;
  R := TRegistry.Create;
  try
    R.RootKey := HKEY_CLASSES_ROOT;
    if R.OpenKeyReadOnly('\' + Ext) then
    begin
      Name := R.ReadString('');
      R.CloseKey;
      if R.OpenKeyReadOnly('\' + Name) then
      begin
        Desc := R.ReadString('');
        R.CloseKey;
        if R.OpenKeyReadOnly('\' + Name + '\shell\open\command') then
        begin
          Command := R.ReadString('');
          R.CloseKey;
          Result := True;
          if R.OpenKeyReadOnly('\' + Name + '\DefaultIcon') then
          begin
            Icon := R.ReadString('');
            R.CloseKey;
          end
          else
            Icon := '';
        end;
      end;
    end;
  finally
    R.Free;
  end;
end;

function SetAssociation(const Ext: String; const Name, Desc, Command, Icon: String): Boolean;
var
  R: TRegistry;
begin
  Result := False;
  R := TRegistry.Create;
  try
    R.RootKey := HKEY_CLASSES_ROOT;
    if R.OpenKey('\' + Ext, True) then
    begin
      R.WriteString('', Name);
      R.CloseKey;
      if R.OpenKey('\' + Name, True) then
      begin
        R.WriteString('', Desc);
        R.CloseKey;
        if R.OpenKey('\' + Name + '\shell\open\command', True) then
        begin
          R.WriteString('', Command);
          R.CloseKey;
          Result := True;
          if (Icon <> '') and R.OpenKey('\' + Name + '\DefaultIcon', True) then
          begin
            R.WriteString('', Icon);
            R.CloseKey;
          end;
        end;
      end;
    end;
  finally
    R.Free;
  end;
end;

An example of usage:

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  Name, Desc, Command, Icon: String;
begin
  if GetAssociation('.txt', Name, Desc, Command, Icon) then
  begin
    Memo1.Lines.Add(Name);
    Memo1.Lines.Add(Desc);
    Memo1.Lines.Add(Command);
    Memo1.Lines.Add(Icon);
  end;
end;


Hope that it helps.
Kambiz
Back to top
View user's profile Send e-mail Visit poster's website
Display posts from previous:   
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2.0.6 © 2001, 2002 phpBB Group