如何将Delphi程序与文件类型相关联,但仅适用于当前用户? [英] How to associate a Delphi program with a file type, but only for the current user?

查看:126
本文介绍了如何将Delphi程序与文件类型相关联,但仅适用于当前用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我无法将程序与特定的文件类型相关联,而不会强制穷人输入其管理员密码(对于家庭用户来说可能是可以的,但对于企业环境中的用户来说,这是一个巨大的问题)。在这种情况下,唯一的解决方案是仅为当前用户进行关联。

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 problem for users in a corporate env). In this case the only solution is to make the association only for the current user.

我已经尝试过,但有些工作不起作用。

I have tried that but something is not working.

如果我理解正确,我必须在ctCurUserFileExt中写一个关键字(就是说)'.mp3',并写入'my_file'。然后在ctCurUserClases中,我添加一个这样的键:

If i understand correctly I have to write a key like (let's say) '.mp3' in ctCurUserFileExt and write in it something like 'my_file'. Then in ctCurUserClases I add a key like this:

WriteReg_String(RootKey, ctCurUserClases+ 'my_file\shell\open\command', '', Application.ExeName+ ' "%L"', TRUE) 

然而,当我双击该文件,Windows会询问应该打开哪个应用程序。

However, when I double click the file, Windows asks me with which application should it open it.

这是常数:

CONST
     RootKey= 'HKEY_CURRENT_USER';
     ctCurUserFileExt= '\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\';
     ctCurUserClases = '\Software\Classes\';


推荐答案

如果要为每个用户注册关联,将您的数据写入

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

这是如何做后者的: / p>

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);

这将关联称为我自己的文本文件类型的.myfile文件,以便他们有notepad.exe的图标将被notepad.exe打开。最后一行告诉资源管理器自己重新加载以反映对文件关联所做的更改。例如,Explorer文件列表视图将更新。 WinAPI函数 SHChangeNotify ShlObj.pas 中声明,所以您需要使用ShlObj

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.

请注意, shell\中的%1 open\command 将展开到当前文件。例如,如果您双击 C:\some dir\test.myfile ,则Explorer将执行命令

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"

这篇关于如何将Delphi程序与文件类型相关联,但仅适用于当前用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆