需求很简单:双击我的一个自定义扩展名的文件,打开相应的应用程序进行处理.查了一些资料,终于把它弄出来,在这里记录一下,以备查用.

①用到"Microsoft.Win32"命名空间.

using Microsoft.Win32;

using Microsoft.Win32;

② 检查一下文件类型是否已被注册.

public static bool FileTypeRegistered(string extension)
{
            RegistryKey sluKey = Registry.ClassesRoot.OpenSubKey(extension);
            if (sluKey != null)
                return true;
            return false;
}

③ 删除已被注册的键值

public static void UnRegistFileType(string extension)
{
            if (FileTypeRegistered(extension))
            {
                Registry.ClassesRoot.DeleteSubKey(extension); 
                string relationName = extension.Substring(1, extension.Length - 1).ToUpper() + " FileType";
                Registry.ClassesRoot.DeleteSubKeyTree(relationName);
                Registry.ClassesRoot.Close();
            }
}

④ 注册自定义文件,并与自己的应用程序相关联

public static void RegistFileType(string extension)
{
            UnRegistFileType(extension);
            string relationName = extension.Substring(1, extension.Length - 1).ToUpper() + " FileType";
            RegistryKey sluKey = Registry.ClassesRoot.CreateSubKey(extension);
            sluKey.SetValue("", relationName);
            sluKey.Close();

RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName);
            relationKey.SetValue("", "Your Description");

RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");//图标
            iconKey.SetValue("", System.Windows.Forms.Application.StartupPath + @"\mainICO.ico");

RegistryKey shellKey = relationKey.CreateSubKey("Shell");
            RegistryKey openKey = shellKey.CreateSubKey("Open");
            RegistryKey commandKey = openKey.CreateSubKey("Command");
            commandKey.SetValue("", System.Windows.Forms.Application.ExecutablePath + " %1");//是数字"1",双击文件之后就把文件的路径传递过来了.
            relationKey.Close();
}

⑤ 修改默认Main()函数,使之能接收参数

[STAThread]
static void Main(string[] args)
{
            if (args != null && args.Length > 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm(args[0]));
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
}

⑥在主窗体构造函数中实现自己的业务逻辑...

不知道大家有没有更好的解决方案,一起学习一下.

转载于:https://www.cnblogs.com/donghaiyiyu/archive/2008/08/11/1265449.html

WinForm 中自定义文件与自己的应用程序相关联相关推荐

  1. Winform中自定义xml配置文件后对节点进行读取与写入

    场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...

  2. winform 中自定义有透明度的panel,可用于加载遮盖层

    在winform 中自定义有透明度的panel,可用于加载遮盖层,代码如下: public class ExtendedPanel : Panel{private const int WS_EX_TR ...

  3. 如何在Windows资源管理器中自定义文件夹背景和图标

    We've covered a lot of Windows tweaks over the years, and today we're going to show you how to custo ...

  4. 在VSCode中自定义文件类型和扩展名关联

    目录 标题 概述 实现步骤 总结 标题 在 VSCode 中自定义文件类型和扩展名关联 Customize file type and extension associations in VSCode ...

  5. Winform中自定义添加ZedGraph右键实现设置所有Y轴刻度的上下限

    场景 Winforn中实现ZedGraph自定义添加右键菜单项(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  6. Winform中自定义ZedGraph右键复制成功后的提示

    场景 Winform中实现ZedGraph中曲线右键显示为中文: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100115292 ...

  7. Winform中实现文件另存为后并打开文件

    场景 在Winform程序中点击按钮实现Excel导出,会打开对话框,文件另存为之后自动打开文件. Process.Start Method 重载 Start()  启动(或重用)此 Process ...

  8. eclipse中自定义视图_如何在Windows中自定义文件夹视图设置

    eclipse中自定义视图 While the Windows File Explorer seems somewhat simplified compared to older versions, ...

  9. Winform中实现文件批量更名器(附代码下载)

    场景 对一个文件夹中的文件进行某种格式的重命名 比如下面文件夹内的文件 程序运行效果 点击文件-打开,打开此文件夹后然后Ctrl+a全选此文件夹所有文件,点击打开 然后在序号设置中可以选择预设模板和起 ...

最新文章

  1. 升级TortoiseSVN-1.9.0.26652-x64-svn-1.9.0导致错误提示
  2. 阿里新晋 CNCF TOC 委员张磊:“云原生”为什么对云计算生态充满吸引力?
  3. 计算机毕业论文多久,计算机毕业论文
  4. 前端开发:小程序--第一步
  5. mysql 静态表 是不是 myisam_mysql的静态表和动态表的区别,MyISAM和InnoDB的区别
  6. 电脑主机前置耳机插孔没声音——解决办法
  7. 毕业论文ppt的研究方法及过程计算机专业,毕业论文答辩ppt(要求和制作技巧)...
  8. 天猫整站(简易版)SSM(十四)需要登录才能使用的功能
  9. 揭秘Apple Watch心率监测技术
  10. 什么软件可以测试手长,手相测试扫一扫软件
  11. 计算机中时间服务是哪个,电脑时间不对 Windows时间服务未运行的解决办法
  12. 浅释丹道筑基功―—―混元桩【转载】
  13. 10步Navicat for Mysql12.1.20破解激活方法(适用于Navica12.1系列软件)
  14. lotus miner 元数据 删除 重建
  15. php x.509,php – 解析X509证书
  16. HTTPS 与 SSL
  17. img不拉伸图片 对图片保留原始比例
  18. [西门子/博途]关于FC5/50,FC6/60的一些经验坑
  19. Python气象绘图之Cartopy
  20. QT5.9实现一个视频播放器播放 暂停进度条

热门文章

  1. python基础小结(麻瓜图文总结版)
  2. python删除行_python 删除文件中指定行
  3. Linux下辅助DNS的搭建以及远程和加密更新
  4. Linux下的虚拟机安装
  5. python调用hive与java调用区别_python和hive结合使用
  6. 一键开启微信“开关头像”,快试试!
  7. Cover团队在Kovan以太坊测试网部署xCOVER智能合约
  8. SAP License:制造企业信息化新动向
  9. pyspider 数据存储到mongoDB中
  10. UGUI- 单列列表(VerticalLayoutGroup)