如何将程序与文件类型关联,但仅限当前用户?

发布于 2024-11-14 07:35:02 字数 658 浏览 6 评论 0原文

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

我已经尝试过了,但有些东西不起作用。

如果我理解正确的话,我必须在 ctCurUserFileExt 中编写一个类似(比方说)“.mp3”的密钥,并在其中写入类似“my_file”的内容。然后在 ctCurUserClases 中我添加一个如下所示的键:

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

但是,当我双击该文件时,Windows 会询问我应该使用哪个应用程序打开它。

这是常数:

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

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.

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) 

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

Here are the constant:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

儭儭莪哋寶赑 2024-11-21 07:35:02

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

HKEY_LOCAL_MACHINE\Software\Classes

如果您只想为当前用户注册关联,请将您的数据写入

HKEY_CURRENT_USER\Software\Classes

这是后者的操作方法:

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 文件,称为“My非常自己的文本文件类型”,这样它们将具有 notepad.exe 的图标,并由 notepad.exe 打开。最后一行告诉资源管理器“重新加载”自身以反映对文件关联所做的更改。例如,资源管理器文件列表视图将更新。 WinAPI函数SHChangeNotifyShlObj.pas中声明,因此您需要使用ShlObj

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

C:\WINDOWS\notepad.exe "C:\some dir\test.myfile"

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"
纸短情长 2024-11-21 07:35:02

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文