C#关联自定义文件类型到应用程序并实现自动导入功能
作者:安替-AnTi 时间:2023-06-22 20:11:11
这几天开发遇到一个需求:
将自定义文件后缀添加默认图标双击
自定义的文件后缀可以自动关联到指定的应用程序并自动打开
上述第一、二两点其实是一个需求,本质是和注册表有关的操作,在注册表中写入默认图标、默认文件后缀、指定打开文件,一旦明白这个思路代码就非常清晰了。
代码中我们写入了两个注册表。
第一个结构如下图所示,其中DefaultIcon定义的是默认图标地址,Command中存放的是指定识别的软件的运行exe路径。
第二个注册表如下图所示,将默认文件后缀和第一个注册表进行关联,这样就可以进行识别。
添加进去之后,我们发现双击文件可以打开软件,但是默认图标并没有刷新,经过查询,需要重启,但这样不是太low了吗,难道每个用户安装一次软件都要重启吗,这时候就需要实现自动对注册表进行刷新,直接引用即可,代码如下。
[DllImport("shell32.dll")]
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
namespace AppC
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
/// 是否通过关联打开的软件
if (args.Length > 0)
{
string path="";
for(int i=0;i<args.Length;i++)
path+=args[i]+" ";
path=path.TrimEnd(' ');
Console.WriteLine(path);
Console.ReadKey();
//Application.Run(new mainForm(path));
}
else
{
string keyName;
string keyValue;
keyName = "C2File";
keyValue = "C2文件";
RegistryKey isExCommand = null;
bool isCreateRegistry = true;
try
{
/// 检查 文件关联是否创建
isExCommand = Registry.ClassesRoot.OpenSubKey(keyName);
if (isExCommand == null)
{
isCreateRegistry = true;
}
else
{
if (isExCommand.GetValue("Create").ToString() == Application.ExecutablePath.ToString())
{
isCreateRegistry = false;
}
else
{
Registry.ClassesRoot.DeleteSubKeyTree(keyName);
isCreateRegistry = true;
}
}
}
catch (Exception)
{
isCreateRegistry = true;
}
/// 假如 文件关联 还没有创建,或是关联位置已被改变
//if (isCreateRegistry)
{
try
{
RegistryKey key, keyico;
key = Registry.ClassesRoot.CreateSubKey(keyName);
key.SetValue("Create", Application.ExecutablePath.ToString());
keyico = key.CreateSubKey("DefaultIcon");
keyico.SetValue("", Application.ExecutablePath + ",0");
/*
string icoFind = Path.Combine("Resources", "C2", "Icon");
string icoFile = Path.Combine(System.Windows.Forms.Application.StartupPath, icoFind, "Icon.ico");
iconKey.SetValue(String.Empty, icoFile);
*/
key.SetValue("", keyValue);
key = key.CreateSubKey("Shell");
key = key.CreateSubKey("Open");
key = key.CreateSubKey("Command");
/// 关联的位置
key.SetValue("", Application.ExecutablePath.ToString() + @" %1/");
/// 关联的文件扩展名,
keyName = ".c2";
keyValue = "C2File";
key = Registry.ClassesRoot.CreateSubKey(keyName);
key.SetValue("", keyValue);
}
catch (Exception)
{
}
}
//Application.Run(new mainForm(""));
}
}
}
}
但是还有一个问题,就是打包,由于我们对注册表的写入是写在Main函数里的,如果用Visual Studio自带的工具进行打包的话,也就是说每次用户在打开软件都会进行一次注册表读写的判断,虽然用户不感知,但是这样并不好。所以我们并不打算将注册表的写入放在Main函数中,而是使用Inno Script Studio这个工具进行美化,将注册表的写入放在里面,这样用户只会在安装的时候写入注册表,删除软件注册表也会自动清空。
#define MyAppExeName "软件.exe"
#define AppIconName "默认图标路径"
[Registry]
Root:HKCU;Subkey: "{#MyRegInstallPath_sk}" ; ValueType:string; ValueName:"{#MyRegInstallPath_vn}"; ValueData:"{app}";Flags:uninsdeletekeyifempty
Root: HKCR; Subkey: ".c2"; Flags: uninsdeletekey
Root: HKCR; Subkey: ".c2"; ValueType: string; ValueName: ""; ValueData: "C2File"
Root: HKCR; Subkey: "C2File"; Flags: uninsdeletekey
Root: HKCR; Subkey: "C2File\DefaultIcon"; Flags: uninsdeletekey
Root: HKCR; Subkey: "C2File\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\{#AppIconName}"; Flags:
Root: HKCR; Subkey: "C2File\shell"; Flags: uninsdeletekey
Root: HKCR; Subkey: "C2File\shell\open"; Flags: uninsdeletekey
Root: HKCR; Subkey: "C2File\shell\open\command"; Flags: uninsdeletekey
Root: HKCR; Subkey: "C2File\shell\open\command"; ValueType: string; ValueName: ""; ValueData: "{app}\{#MyAppExeName} ""%1"""; Flags:
来源:https://blog.csdn.net/weixin_35770067/article/details/120086481
标签:c#,应用程序,自动导入,自定义文件类型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Security OAuth 自定义授权方式实现手机验证码
2021-10-03 23:30:27
![](https://img.aspxhome.com/file/2023/2/72282_0s.png)
spring缓存cache的使用详解
2023-03-28 11:36:14
![](https://img.aspxhome.com/file/2023/8/65328_0s.png)
java9迁移注意问题总结
2022-07-19 11:26:30
AjaxControlToolkit AjaxFileUpload 显示英文改成中文的解决方法
2021-06-01 21:09:57
![](https://img.aspxhome.com/file/2023/5/106425_0s.gif)
C# partial关键字说明
2021-12-29 15:29:54
Spring Boot学习入门之AOP处理请求详解
2023-11-27 10:55:17
JAVA的反射机制你了解多少
2023-11-29 16:46:38
![](https://img.aspxhome.com/file/2023/9/60609_0s.jpg)
c#中判断字符串是不是数字或字母的方法
2023-02-10 06:08:49
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
利用Kotlin开发你的第一个Android应用
2022-04-23 14:39:53
![](https://img.aspxhome.com/file/2023/5/100625_0s.png)
Fluent Mybatis实际开发中的优势对比
2022-07-09 14:29:34
C#利用正则判断输入是否为纯数字、容器类
2022-10-10 03:00:39
![](https://img.aspxhome.com/file/2023/3/83343_0s.gif)
Java使用通配符实现增强泛型详解
2021-07-06 17:28:01
![](https://img.aspxhome.com/file/2023/2/60282_0s.png)
Java的关键字与保留字小结
2021-05-30 10:20:50
Java的字符串中对子字符串的查找方法总结
2022-12-16 17:16:31
Java 实战项目锤炼之小区物业管理系统的实现流程
2021-07-17 09:24:01
![](https://img.aspxhome.com/file/2023/2/125252_0s.png)
Java+Nginx实现POP、IMAP、SMTP邮箱代理服务
2023-11-26 10:31:47
![](https://img.aspxhome.com/file/2023/7/59327_0s.jpg)
java统计字符串中重复字符出现次数的方法
2022-02-15 00:52:16
Android编程实现启动界面的方法分析
2022-02-18 14:25:27
![](https://img.aspxhome.com/file/2023/4/139364_0s.jpg)
Spring Boot 集成PageHelper的使用方法
2021-10-04 19:16:30
![](https://img.aspxhome.com/file/2023/9/113389_0s.webp)