Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Associating files programatically

Posted on 2012-12-22
6
Medium Priority
?
392 Views
Last Modified: 2012-12-23
Hi all.

I have a small program which I use when I have to restore some file association silently modified by a new program installed. For instance I have recently installed VideoPad and it has associated many video files with itself without prompting for this. The code (working perfectly some time ago) is something like:

Extension := '.avi';
Prog := 'C:\Program files\VideoLAN\VLC\vlc.exe';

procedure writeAssociation(Extension, Prog: string);
var
  reg: TRegistry;
  Log: string;
begin
  Log := '';
  reg := TRegistry.Create;
  try
    try
      reg.RootKey := HKEY_CLASSES_ROOT;
      reg.LazyWrite := false;
      reg.OpenKey(Extension + '\shell\open\command', true);
      reg.WriteString('', Prog + ' %1');
      reg.CloseKey;
      reg.OpenKey(Extension + '\DefaultIcon', true);
      reg.WriteString('', Prog + ',0');
      reg.CloseKey;
      Log := 'File type ' + Extension + ' is now associated with ' + Prog;
    except
      Log := 'Association failed for file type ' + Extension;
    end;
  finally
    reg.free;
  end;
  mmo1.Lines.Add(Log);
end;

Open in new window


Now this code changes the registry values correctly, but avi files are still opened by VideoPad when I double-click on them and the icon is not vlc icon but VideoPad one.

Now, my problem is not to reassociate the avi extension with vlc: I can do it manually. My problem is how to write a program which set the files associations in the registry and makes these associations be respected by Windows. That is, I have to understand why the registry values set by my code above are ignored by Windows.

Hope to have been clear.

Thanks in advance for any advice.

Cheers
Marco
0
Comment
Question by:Marco Gasi
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:accountsworlds
ID: 38715720
1) I wanted the option to "open with" from all file types so i added
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CLASSES_ROOT;
  reg.LazyWrite := false;
  reg.OpenKey('*\OpenWithList\EncryptionSystem', true);
  reg.WriteString('','C:\Program Files\EncryptionSystem\EncryptionSystem.exe "%1"');
  reg.CloseKey;
  reg.free;

Open in new window

If I look in the registry using regedit it's sure there as it should seem to be but when I right click on a file and select Open With it's not there...

So I then added this
 
 reg.OpenKey('*\shell\Encrypt\command', true);
  reg.WriteString('','C:\Program Files\EncryptionSystem\EncryptionSystem.exe "%1"');
  reg.CloseKey;

Open in new window


Source: http://stackoverflow.com/questions/590892/windows-registry-file-association-menu
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 38715802
Sorry, but i really don't see a solution in the source you posted.
0
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 38716252
try my example:

uses ...  Registry, ShlObj;

procedure AssocFileExt(sExt, sFileName:String);
var
  reg: TRegistry;
  sExtWOPoint: String;
  buf: Byte;
begin
  sExt := Trim(sExt);
  if Length(sExt) = 0 then exit;
  sExtWOPoint := sExt;
  if sExtWOPoint[1] = '.' then Delete(sExtWOPoint, 1, 1);
  buf := 0;
  
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CLASSES_ROOT;
    if reg.OpenKey('\'+sExt, True) then
    begin
      reg.WriteString('', sExtWOPoint + 'file');
      reg.CloseKey;
      reg.OpenKey('\'+sExt+'\OpenWithList\ehshell.exe\', True);
      reg.CloseKey;
      if reg.OpenKey('\'+sExt+'\OpenWithProgIds\', True) then
      begin
        reg.WriteBinaryData(sExtWOPoint + 'file', buf, SizeOf(buf));
      end;
      reg.CloseKey;
      if reg.OpenKey('\'+sExtWOPoint + 'file\shell\open\command\', True) then
        reg.WriteString('', '"'+sFileName+'" "%1"');
      reg.CloseKey;
    end;

    reg.RootKey := HKEY_CURRENT_USER;
    if reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\'+sExt+'\OpenWithProgids\', True) then
    begin
      reg.WriteBinaryData('avifile', buf, SizeOf(buf));
    end;
  finally
    reg.Free;
  end;
  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
end;

....
AssocFileExt('.avi', 'C:\Program files\VideoLAN\VLC\vlc.exe');

Open in new window


... and here is another example

... for per user ext registration (and avoid admin rules) there is similar values under
HKEY_CURRENT_USER\SOFTWARE\Classes
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Author Comment

by:Marco Gasi
ID: 38717045
Hi sinisav, ho do you do? :-)

Yes your code works: I didn't test it yet, but I know beacause I understood that I can simply empty the data of the default value to make the new association work.

Many thanks: you're becoming my personal expert ;-)

Merry Christmas and Happy New Year!
0
 
LVL 31

Author Closing Comment

by:Marco Gasi
ID: 38717046
Merry Christmas and Happy New Year!
0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 38717054
Merry Christmas and successful New Year to you, your family and to all who comes to experts-exchange to expand their knowledge or share it as well.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

886 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