Solved

Associating files programatically

Posted on 2012-12-22
6
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 27

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
Independent Software Vendors: 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 27

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month5 days, 10 hours left to enroll

626 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