Solved

Associating files programatically

Posted on 2012-12-22
6
384 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
Industry Leaders: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: sending SMS on android platform 1 102
MS Access from Delphi 31 100
firemonkey Android Listview Sort items 7 103
Browsing a TTreeView in Delphi 5 48
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 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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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