Solved

Associating files programatically

Posted on 2012-12-22
6
375 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 30

Author Comment

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

Accepted Solution

by:
Sinisa Vuk earned 500 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

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 30

Author Closing Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now