C#设置自定义文件图标实现双击启动(修改注册表)

这篇文章介绍的是利用C#设置自定义文件图标,然后实现双击启动的功能,文章给出了示例代码,介绍的很详细,有需要的可以参考借鉴。

程序生成的自定义文件,比如后缀是.test

这种文件怎么直接启动打开程序,并打开本文件呢

     1、双击打开

     2、自定义的文件,有图标显示

     3、自定义的文件,点击右键有相应的属性

后台代码:(如何在注册表中修改信息)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//工具启动路径
string toolPath = System.Windows.Forms.Application.StartupPath + "\\邮件小工具.exe" ;
 
string extension = SptdConst.FileExtension;
 
string fileType = "Email File" ;
 
string fileContent = "text/plain" ;
//获取信息
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension);
 
if (registryKey != null && registryKey.OpenSubKey( "shell" ) != null && registryKey.OpenSubKey( "shell" ).OpenSubKey( "open" ) != null &&
   registryKey.OpenSubKey( "shell" ).OpenSubKey( "open" ).OpenSubKey( "command" ) != null )
{
   var varSub = registryKey.OpenSubKey( "shell" ).OpenSubKey( "open" ).OpenSubKey( "command" );
   var varValue = varSub.GetValue( "" );
 
   if (Object.Equals(varValue, toolPath + " %1" ))
   {
     return ;
   }
}
//删除
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(extension, false );
//文件注册
registryKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension);
registryKey.SetValue( "文件类型" , fileType);
registryKey.SetValue( "Content Type" , fileContent);
//设置默认图标
Microsoft.Win32.RegistryKey iconKey = registryKey.CreateSubKey( "DefaultIcon" );
iconKey.SetValue( "" , System.Windows.Forms.Application.StartupPath + "\\logo.ico" );
//设置默认打开程序路径
registryKey = registryKey.CreateSubKey( "shell\\open\\command" );
registryKey.SetValue( "" , toolPath + " %1" );
//关闭
registryKey.Close();

在修改了注册表信息后,双击文件是启动了软件,之后怎么在代码中操作?

?
1
2
3
4
5
6
7
8
9
10
11
//双击启动打开
  //如果原有路径中存在空格,则会分解成多个元素
if (e.Args.Length > 0)
{
    string filePath = String.Join( " " , e.Args.ToArray());
    FileInfo file = new FileInfo(filePath);
    if (file.Exists)
     {
       EmailToolConst.DoubleClickSptdFilePath = file.FullName;
     }
  }

然后可以在主程序loaded方法中,判断DoubleClickSptdFilePath 是否有值,如果有,则获取路径下的文件,继续操作。

以上就是小编为大家整理的C#设置自定义文件图标实现双击启动的全部内容,希望这篇文章的内容对大家的学习或者工作能有一定的帮助,如果有疑问可以留言交流。


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值