用 assoc 和 ftype 查看文件关联

两个被遗忘的 CMD 内置命令:扩展名 → 文件类型 → 可执行命令的两级映射。

Windows配置命令提示符开发中风险
运行 assoc .ext 查看文件类型名,再用 ftype <类型名> 查看启动命令;用 ftype 名称="命令" %1 %* 设置。

01命令

命令
$ assoc .py ; ftype Python.File

02作用说明

关联分两级:assoc 把扩展名(如 .py)映射到类型名,ftype 把类型名映射到实际命令行,%1 代表文件本身,%* 代表附加参数。先 assoc 后 ftype 的组合是脚本注册自定义处理器的标准做法,如 assoc .log=MyLog 加 ftype MyLog=notepad.exe %1。这些信息存于 HKCR 注册表,ftype 本质上是注册表快捷方式。

03风险

风险中风险
使用条件Administrator rights to change system-wide associations
!注意: 新版 Windows 常被每用户和界面层关联覆盖——“设置”应用中的选择优先于 assoc/ftype 的设置,修改后需实际验证。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • ASSOC - Windows CMD
    SS64 · technical · 2026-09-17
    依据: SS64 explains assoc mapping extensions to file types and ftype mapping file types to commands, with examples of setting both.
  • ftype | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    依据: Microsoft documents ftype displaying or modifying file type command strings used by assoc, including %1 %* argument substitution.

最后验证: 2026-09-17 · 分类: 开发者

06相关方案