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

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

问题描述

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

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

这是后者的操作方法:

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打开。最后一行告诉资源管理器自己重新加载以反映对文件关联所做的更改。例如,资源管理器文件列表视图将更新。 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.

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

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"

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

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