How to associate a program with a file type, but only for the current user?

前端 未结 2 911
清酒与你
清酒与你 2020-12-06 01:29

So, I cannot associate my program with a specific file type without forcing the poor user to enter its admin password (it may be ok for home users, but it is a gigantic prob

相关标签:
2条回答
  • 2020-12-06 01:44

    Have you looked at setting it under HKEY_CURRENT_USER\Software\Classes as per http://support.microsoft.com/kb/257592

    0 讨论(0)
  • 2020-12-06 01:48

    If you want to register the association for every user, write your data to

    HKEY_LOCAL_MACHINE\Software\Classes
    

    If you want to register the association for the current user only, write your data to

    HKEY_CURRENT_USER\Software\Classes
    

    This is how to do the latter:

    with TRegistry.Create do
      try
        RootKey := HKEY_CURRENT_USER;
        if OpenKey('\Software\Classes\.myfile', true) then
          WriteString('', 'MyAppDataFile');
        if OpenKey('\Software\Classes\MyAppDataFile', true) then
          WriteString('', 'My Very Own Text File Type');
        if OpenKey('\Software\Classes\MyAppDataFile\DefaultIcon', true) then
          WriteString('', 'C:\WINDOWS\notepad.exe');
        if OpenKey('\Software\Classes\MyAppDataFile\shell\open\command', true) then
          WriteString('', 'C:\WINDOWS\notepad.exe "%1"');
      finally
        Free;
      end;
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
    

    This will associate .myfile files, called "My Very Own Text File Type" so that they will have the icon of notepad.exe and will be opened by notepad.exe. The last line tells Explorer to 'reload' itself to reflect the changes made to the file associations. For instance, Explorer file list views will update. The WinAPI function SHChangeNotify is declared in ShlObj.pas, so you need to uses ShlObj.

    Notice that the %1 in shell\open\command will expand to the current file. For instance, if you double-click on C:\some dir\test.myfile, then Explorer will execute the command

    C:\WINDOWS\notepad.exe "C:\some dir\test.myfile"
    
    0 讨论(0)
提交回复
热议问题