Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Associating files programatically

Posted on 2012-12-22
6
Medium Priority
?
390 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 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
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.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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