使用我自己的应用程序为 Windows10 中的当前用户 register/unregister 文件类型的最先进方法?

State of the art way to register/unregister a file type with my own application for the current user in Windows10?

我找到了几种方法来在我自己的应用程序中注册文件类型(在 Windows Explorer 中双击该文件类型会将该文件加载到我的应用程序中)。有几个很老了,好像被弃用了,还有一些不清楚,缺乏实际的使用例子,因此容易产生误解。

  1. @AndreasRejbrand 已在此处发布了一个可接受的解决方案: How to associate a program with a file type, but only for the current user?
    a) 没有取消注册文件类型的免费方法。
    b) 不清楚'MyAppDataFile'是什么意思。
    c) 没有实际使用例子

  2. DSiWin32 库包含方法 DSiRegisterUserFileAssocDSiUnregisterUserFileAssoc:
    一)procedure DSiRegisterUserFileAssoc(const extension, progID, description, defaultIcon, openCommand: string);
    aa) 不清楚progID参数是什么意思。
    ab) 不清楚如何传递defaultIcon参数。
    ac) 不清楚如何传递openCommand参数。
    b) procedure DSiUnregisterUserFileAssoc(const progID: string);
    ba) 不清楚progID参数是什么意思,如何格式化。
    c) 有实际使用示例就好了

顺便说一句,这是来自@AndreasRejbrand 的代码:

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

任何人都可以为这两种方法提供清晰的“万无一失”的实际使用示例吗?哪个更好?

编辑:对于“实际使用示例”,我的意思是:procedure/function 带有示例参数。

我不清楚 OP 实际上在问什么,但在评论中有一些澄清请求,我无法在评论中加入这样的解释,所以我创建了一个(可能是临时的)CW在这里回答。

假设您希望所有扩展名为 .stext 的文件在资源管理器中都被称为 Super Text File,以 C:\Program Files\Super Editor\superedit.exe 打开,并具有图标 C:\Program Files\Super Editor\docicon.ico,那么您将使用以下代码:

with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    if OpenKey('\Software\Classes\.stext', true) then
      WriteString('', 'SuperTextFile');
    if OpenKey('\Software\Classes\SuperTextFile', true) then
      WriteString('', 'Super Text File');
    if OpenKey('\Software\Classes\SuperTextFile\DefaultIcon', true) then
      WriteString('', 'C:\Program Files\Super Editor\docicon.ico');
    if OpenKey('\Software\Classes\SuperTextFile\shell\open\command', true) then
      WriteString('', '"C:\Program Files\Super Editor\superedit.exe" "%1"');
  finally
    Free;
  end;
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);

SuperTextFile 字符串作为文件扩展名部分和文件类型部分之间的连接。如果您愿意,可以将其命名为 jmkrfnjk

如果您想在记事本中打开文件,请在最后 WriteString.

中使用 notepad.exe 的路径

这可能不是最复杂的方法(可能是 Win 9x 方法),但它仍然有效,不是吗?如果它不再对 Win32 桌面平台的整个未来继续有效,我会感到惊讶。

DSiRegisterUserFileAssoc 例程与上面的代码完全相同 the same thing,因此两种方法完全等同。

有关最新技术的所有详细信息,请参阅 documentation