如何将应用程序与文件类型(文件扩展名)关联起来?

您所在的位置:网站首页 在windows中记事本程序默认的文件扩展名是 如何将应用程序与文件类型(文件扩展名)关联起来?

如何将应用程序与文件类型(文件扩展名)关联起来?

2023-08-10 08:03| 来源: 网络整理| 查看: 265

自定义一个文件格式,如 .jgrass ,如何将这种文件格式与对应的程序关联起来? 或者,自己编写了一个可以打开 txt 格式的应用程序,怎么能够通过双击 txt 文件,直接打开这个自定义程序?

基本思路是向注册表中写入或修改一些值。 具体可以参见:

如何为你的 Windows 应用程序关联一种或多种文件类型 - walterlv

注册表中的文件扩展名

注册表中的关联程序

举个栗子

e.g. 怎么修改 txt 文件的默认打开格式?

理论上讲,有两种实现方式。 1 修改上图 1 中的 .txt 项的默认值,将其修改为自定义的程序ID,然后在注册表中添加自定义的程序ID,已经其对应的执行程序的路径。 2 修改 txtfile 项中的默认值,直接将其路径修改为自定义程序的路径。

看起来 2 的修改更小,更省事。但这是有问题的。 因为 txtfile 可能不止关联了 .txt 这一种文件格式,还关联了很多其他的格式,直接修改 txtfile 中的值,可能会导致这些文件打不开。 txtfile 这个注册表项,除了程序 NOTEPAD.EXE 的发布者可以修改之外,其他应用都不应该去修改它,此项是对修改封闭的。

而采用方式 1,只会影响 .txt 这一种文件格式的打开方式。在注册表中添加自定义的程序ID,这是一种扩展开放的修改方式。

具体代码

下面是具体代码。

nuget 引用 (来源:nuget.org)

向注册表中注册可执行程序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Win32; using Walterlv.Win32; namespace GrassDemoPark.WPF2.Tiny.RegEdit { /* * [如何为你的 Windows 应用程序关联一种或多种文件类型 - walterlv] * (https://blog.walterlv.com/post/windows-file-type-association.html) */ /// /// 向注册表中注册可执行程序 /// class RegisterProgram { /// /// 需要管理的执行程序的产品ID,(厂商名.应用名.版本号) /// e.g. Microsoft.PowerShellConsole.1 /// public string ProgramId { get; } /// /// 该执行程序所关联文件的类型描述 /// e.g. Text Document /// public string? TypeName { get; set; } /// /// 该执行程序所关联文件的类型描述 /// e.g. 一个神奇的文本文件 /// public string? FriendlyTypeName { get; set; } /// /// 该执行程序所关联文件对应的 Icon /// public string? DefaultIcon { get; set; } /// /// 是否总是显示指定文件类型的扩展名 /// public bool? IsAlwaysShowExt { get; set; } /// /// 该执行程序可执行的操作/谓词 /// public string? Operation { get; set; } /// /// 对应谓词下,其执行的具体命令;仅在有效时,此值才有效 /// public string? Command { get; set; } /// /// 根据指定 ProgramId,创建 的实例。 /// /// public RegisterProgram(string programId) { if (string.IsNullOrWhiteSpace(programId)) { throw new ArgumentNullException(nameof(programId)); } ProgramId = programId; } /// /// 将此文件扩展名注册到当前用户的注册表中 /// public void WriteToCurrentUser() { WriteToRegistry(RegistryHive.CurrentUser); } /// /// 将此文件扩展名注册到所有用户的注册表中。(进程需要以管理员身份运行) /// public void WriteToAllUser() { WriteToRegistry(RegistryHive.LocalMachine); } /// /// 将此文件扩展名写入到注册表中 /// private void WriteToRegistry(RegistryHive registryHive) { // 写 默认描述 registryHive.Write32(BuildRegistryPath(ProgramId), TypeName ?? string.Empty); // 写 FriendlyTypeName if (FriendlyTypeName != null && !string.IsNullOrWhiteSpace(FriendlyTypeName)) { registryHive.Write32(BuildRegistryPath(ProgramId), "FriendlyTypeName", FriendlyTypeName); } // 写 IsAlwaysShowExt if (IsAlwaysShowExt != null) { registryHive.Write32(BuildRegistryPath(ProgramId), "IsAlwaysShowExt", IsAlwaysShowExt.Value ? "1" : "0"); } // 写 Icon if (DefaultIcon != null && !string.IsNullOrWhiteSpace(DefaultIcon)) { registryHive.Write32(BuildRegistryPath($"{ProgramId}\\DefaultIcon"), DefaultIcon); } // 写 Command if (Operation != null && !string.IsNullOrWhiteSpace(Operation)) { registryHive.Write32(BuildRegistryPath($"{ProgramId}\\shell\\{Operation}\\command"), Command ?? string.Empty); } } private string BuildRegistryPath(string relativePath) { return $"Software\\Classes\\{relativePath}"; } } } 向注册表中注册文件扩展名 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Win32; using Walterlv.Win32; namespace GrassDemoPark.WPF2.Tiny.RegEdit { /* * [如何为你的 Windows 应用程序关联一种或多种文件类型 - walterlv] * (https://blog.walterlv.com/post/windows-file-type-association.html) */ /// /// 向注册表中注册文件扩展名 /// class RegisterFileExtension { /// /// 文件后缀名(带.) /// public string FileExtension { get; } /// /// 该后缀名所指示的文件的类型 /// e.g. text/plain /// [MIME 类型 - HTTP | MDN](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types ) /// [Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml ) /// public string? ContentType { get; set; } /// /// 该后缀名所指示的文件的感知类型 /// e.g. text /// [Perceived Types (Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/cc144150(v%3Dvs.85) ) /// public string? PerceivedType { get; set; } /// /// 该后缀名所指示的文件关联的默认应用程序的 ProgramId /// public string? DefaultProgramId { get; set; } /// /// 该后缀名所指示的文件,还可以被哪些 ProgramId 所代表的程序打开。 /// public IList OpenWithProgramIds { get; set; } = new List(); /// /// 根据指定文件扩展名,创建 的实例。 /// /// public RegisterFileExtension(string fileExtension) { if (string.IsNullOrWhiteSpace(fileExtension)) { throw new ArgumentNullException(nameof(fileExtension)); } if (!fileExtension.StartsWith(".", StringComparison.Ordinal)) { throw new ArgumentException( $"{fileExtension} is not a valid file extension. it must start with \".\"", nameof(fileExtension)); } FileExtension = fileExtension; } /// /// 将此文件扩展名注册到当前用户的注册表中 /// public void WriteToCurrentUser() { WriteToRegistry(RegistryHive.CurrentUser); } /// /// 将此文件扩展名注册到所有用户的注册表中。(进程需要以管理员身份运行) /// public void WriteToAllUser() { WriteToRegistry(RegistryHive.LocalMachine); } /// /// 将此文件扩展名写入到注册表中 /// private void WriteToRegistry(RegistryHive registryHive) { // 写默认执行程序 registryHive.Write32(BuildRegistryPath(FileExtension), DefaultProgramId ?? string.Empty); // 写 ContentType if (ContentType != null && !string.IsNullOrWhiteSpace(ContentType)) { registryHive.Write32(BuildRegistryPath(FileExtension), "Content Type", ContentType); } // 写 PerceivedType if (PerceivedType != null && !string.IsNullOrWhiteSpace(PerceivedType)) { registryHive.Write32(BuildRegistryPath(FileExtension), "PerceivedType", PerceivedType); } // 写 OpenWithProgramIds if (OpenWithProgramIds.Count > 0) { foreach (string programId in OpenWithProgramIds) { registryHive.Write32(BuildRegistryPath($"{FileExtension}\\OpenWithProgids"), programId, string.Empty); } } } private string BuildRegistryPath(string relativePath) { return $"Software\\Classes\\{relativePath}"; } } }

原文链接: https://www.cnblogs.com/jasongrass/p/11965647.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3