当前位置: 移动技术网 > IT编程>开发语言>c# > C#设置自定义文件图标实现双击启动(修改注册表)

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

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

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

     1、双击打开

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

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

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

  //工具启动路径
  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();

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

//双击启动打开
 //如果原有路径中存在空格,则会分解成多个元素
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#设置自定义文件图标实现双击启动的全部内容,希望这篇文章的内容对大家的学习或者工作能有一定的帮助,如果有疑问可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网