3

I have a TEditbox where the user keys in some name for the file along with the extension he wants to save it as. Now I want to validate if the extension he entered is a valid extension registered with windows. How can I achieve this?

All I have is:

procedure TForm2.OkBtnClick(Sender: TObject);
var
ExtractedFileExt: string;
begin
  ExtractedFileExt := ExtractFileExt(cxCbxSelectedFile.Text);
end;

How can I use that string variable and check if it is a valid file extension registered with Windows?

Hello Man
  • 693
  • 2
  • 12
  • 29
  • Define what you mean by valid extension. Do you mean a file name with no reserved characters and no `.` character? Or do you mean an extension with a registered association? – David Heffernan Feb 02 '16 at 07:45
  • What do you mean by valid/accepted? You could try AssocQueryString or the like. – Uli Gerhardt Feb 02 '16 at 07:48
  • I mean valid extension which is registered association with windows – Hello Man Feb 02 '16 at 08:02
  • Use AssocQueryString. Perhaps. Do you want to know whether there is an app that can open a file? Or is any form of association fine? I don't think you've really worked out what you want to do yet. – David Heffernan Feb 02 '16 at 08:07
  • 2
    Hm.You could try to run through HKEY_CLASSES_ROOT and check whether the extension matches any .xyz keys there. – Thorsten Dittmar Feb 02 '16 at 08:27
  • 2
    @Thorston You could, but isn't it better to use the API rather than hacking the registry? – David Heffernan Feb 02 '16 at 08:30
  • @DavidHeffernan could you show me a small example on how to use the AssocQueryString and validate if the passed param(the extension) is registered or not. – Hello Man Feb 02 '16 at 08:45
  • Google for "delphi AssocQueryString"??? – Uli Gerhardt Feb 02 '16 at 08:49
  • 1
    @HelloMan This is something we see a lot here. Askers want us to write their code for them. They then take it without understanding and then complain if it doesn't behave as they expect. Since they never understood it in the first place, they are stuck. Really, you should be looking to learn. Read the docs. Look around the web for a variety of examples. Translate them yourself. Explore the code you write. Test it. Poke it with different extensions of different forms and make sure it behaves as you expect. In short, seek learning and understanding rather than code. – David Heffernan Feb 02 '16 at 08:54
  • http://stackoverflow.com/questions/1265099/how-to-find-if-an-document-can-be-opened-via-shellexecute – fantaghirocco Feb 02 '16 at 09:00
  • 1
    Possible duplicate of [Getting File Associations using Windows API](http://stackoverflow.com/questions/3536634/getting-file-associations-using-windows-api) – fantaghirocco Feb 02 '16 at 09:00
  • @DavidHeffernan I am asking for sample reference to the assocquerystring and not the code itself. I cant find useful stuff related to it on google. – Hello Man Feb 02 '16 at 09:02
  • I cannot believe that a websearch won't lead you to many useful hits. It feels like you might be giving up too readily. – David Heffernan Feb 02 '16 at 09:05
  • @HelloMan You gotta be kidding. It's my **first hit** Googling for "delphi AssocQueryString" as Uli suggested. http://www.devsuperpage.com/search/Articles.asp?ArtID=553576 – Jan Doggen Feb 02 '16 at 09:31
  • @DavidHeffernan thanks and i executed the program given and when passed .exe as param, i get the following error: Failed for ASSOCSTR_COMMAND, Error message is The parameter is incorrect Failed for ASSOCSTR_EXECUTABLE, Error message is The parameter is incorrect Failed for ASSOCSTR_FRIENDLYDOCNAME, Error message is The parameter is incorrect ////// this repeats for all params of the assocstr – Hello Man Feb 02 '16 at 09:51
  • 1
    This really doesn't seem terribly constructive. – David Heffernan Feb 02 '16 at 09:52
  • @DavidHeffernan I didn't know the `AssocQueryString` function :-) As it is there and seems to do the same thing, I'd of course use that instead. – Thorsten Dittmar Feb 02 '16 at 10:57
  • I think Thorsten was right, the bare minimum registration is much more accessible by the registry directly. – Free Consulting Feb 02 '16 at 14:44

1 Answers1

1

I think it's not "hacking" the registry. As far as I know, there is no good way to do what you want to do without reading any values from the registry. So, use this code if you want to use the registry:

uses Registry;

function GetProgramAssociation(const Ext: string): string;
var reg: TRegistry;
    s: string;
begin
  s:='';
  reg:=TRegistry.Create;
  try
    reg.RootKey:=HKEY_CLASSES_ROOT;
    if reg.OpenKey('.'+ext+'shellopencommand', false) then
    begin
      s:=reg.ReadString('');
      reg.CloseKey;
    end
    else
    begin
      if reg.OpenKey('.'+ext, false) then
      begin
        s:=reg.ReadString('');
        reg.CloseKey;
        if s='' then
        begin
          if reg.OpenKey(s+'shellopencommand', false) then
            s:=reg.ReadString('');
          reg.CloseKey;
        end;
      end;
    end;
    if Pos('%', s) > 0 then Delete(s, Pos('%', s), length(s));
    if ((length(s)>0) and (s[1]='"')) then Delete (s, 1, 1);
    if ((length(s)>0) and (s[length(s)]='"')) then Delete(s, Length(s), 1);
    while ((length(s)>0) and ((s[length(s)]=#32) or (s[length(s)]='"'))) do
      Delete(s, Length(s), 1);
    result:=s;
  finally
  reg.Free;
  end;
end;

And then:

if GetProgramAssociation(Extension) = '' then
  ShowMessage('Nope!');

It works fine. It returns an empty string if the Extension is not associated with a valid program. For example if you enter 'doc' (without '.') it returns Word.Document.8 and if you enter 'abcdef' it returns nothing ('').

Don't forget: put in the extension without a dot

renehsz
  • 43
  • 6