C# 文件关联类






4.21/5 (15投票s)
允许您快速创建、编辑和删除注册表中的文件关联。文件扩展名的默认图标、描述、可执行应用程序等。
引言
我在这里提供了一个小型库,允许您设置文件关联。文件关联是注册表中多个键的集合,用于设置文件类型使用哪个程序打开,在文件属性对话框中显示的文件类型描述,文件类型上显示的图标,甚至在 Windows 找不到打开文件类型的程序时打开的对话框中显示的程序(“打开方式”列表)。
背景
我使用 C# 大约一年了……我之前在开发一个具有前所未见功能的大型文字处理器项目时遇到了文件关联问题。所以我做了一些研究,并弄清楚了如何做到这一点。
Using the Code
顶部的下载包含一个 CS 文件和一个 DLL 文件。CS 文件包含我的库的代码,以便您可以了解其工作原理,而 DLL 用于在您的程序中添加引用。下面的代码块显示了引用它后如何使用该库。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AF_Lib.IO.Associations;
public class AFFileAssociations_Example
{
public static void Main()
{
// Initializes a new AF_FileAssociator to associate the .ABC file extension.
AF_FileAssociator assoc = new AF_FileAssociator(".abc");
// Creates a new file association for the .ABC file extension.
// Data is overwritten if it already exists.
assoc.Create("My_App",
"My application's file association",
new ProgramIcon(@"C:\Program Files\My_App\icon.ico"),
new ExecApplication(@"C:\Program Files\My_App\myapp.exe"),
new OpenWithList(new string[] { "My_App" }));
// Gets each piece of association info individually, all as strings.
string id = assoc.ID;
string description = assoc.Description;
string icon = assoc.DefaultIcon.IconPath;
string execApp = assoc.Executable.Path;
string[] openWithList = assoc.OpenWith.List;
// Sets each piece of association info individually.
ProgramIcon newDefIcon = new ProgramIcon(@"C:\Program Files\My_App\icon2.ico");
ExecApplication newExecApp =
new ExecApplication(@"C:\Program Files\My_App\myapp2.exe");
OpenWithList newOpenWith = new OpenWithList(new string[] { "myapp2.exe" });
assoc.ID = "My_App_2";
assoc.Description = "My application's file association #2";
assoc.DefaultIcon = newDefIcon;
assoc.Executable = newExecApp;
assoc.OpenWith = newOpenWith;
// Gets the extension of the associator that was set when initializing it.
string extension = assoc.Extension;
// Deletes any keys that were associated with the .ABC file extension.
assoc.Delete();
}
}
关注点
这个库应该非常有用。此项目的附件包含我的代码文件和引用它的 DLL。请在下方留下任何问题或评论。感谢阅读我的文章!
历史
- 2009 年 11 月 10 日:初始发布
- 2009 年 11 月 11 日:更新源代码