使用C#在程序运行时创建文件关联

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using Microsoft.Win32;
  5. namespace AppC
  6. {
  7.     static class Program
  8.     {
  9.         /// <summary>
  10.         /// 应用程序的主入口点。
  11.         /// </summary>
  12.         [STAThread]
  13.         static void Main(string[] args)
  14.         {
  15.             Application.EnableVisualStyles();
  16.             Application.SetCompatibleTextRenderingDefault(false);
  17.             /// 是否通过关联打开的软件
  18.             if (args.Length > 0)
  19.                 Application.Run(new mainForm(args[0].ToString()));
  20.             else
  21.             {
  22.                 string keyName;
  23.                 string keyValue;
  24.                 keyName = "WPCFile";
  25.                 keyValue = "资源包文件";
  26.                 RegistryKey isExCommand = null;
  27.                 bool isCreateRegistry = true;
  28.                 try
  29.                 {
  30.                     /// 检查 文件关联是否创建
  31.                     isExCommand = Registry.ClassesRoot.OpenSubKey(keyName);
  32.                     if (isExCommand == null)
  33.                     {
  34.                         isCreateRegistry = true;
  35.                     }
  36.                     else
  37.                     {
  38.                         if (isExCommand.GetValue("Create").ToString() == Application.ExecutablePath.ToString())
  39.                         {
  40.                             isCreateRegistry = false;
  41.                         }
  42.                         else
  43.                         {
  44.                             Registry.ClassesRoot.DeleteSubKeyTree(keyName);
  45.                             isCreateRegistry = true;                            
  46.                         }
  47.                     }
  48.                 }
  49.                 catch (Exception)
  50.                 {
  51.                     isCreateRegistry = true;
  52.                 }
  53.                 /// 假如 文件关联 还没有创建,或是关联位置已被改变
  54.                 if (isCreateRegistry)
  55.                 {
  56.                     try
  57.                     {
  58.                         RegistryKey key, keyico;
  59.                         key = Registry.ClassesRoot.CreateSubKey(keyName);
  60.                         key.SetValue("Create", Application.ExecutablePath.ToString());
  61.                         keyico = key.CreateSubKey("DefaultIcon");
  62.                         keyico.SetValue("", Application.ExecutablePath + ",0");
  63.                         key.SetValue("", keyValue);
  64.                         key = key.CreateSubKey("Shell");
  65.                         key = key.CreateSubKey("Open");
  66.                         key = key.CreateSubKey("Command");
  67.                         /// 关联的位置
  68.                         key.SetValue("""/"" + Application.ExecutablePath.ToString() + "/" /"%1/"");
  69.                         /// 关联的文件扩展名, 
  70.                         keyName = ".wpc";
  71.                         keyValue = "WPCFile";
  72.                         key = Registry.ClassesRoot.CreateSubKey(keyName);
  73.                         key.SetValue("", keyValue);
  74.                     }
  75.                     catch (Exception)
  76.                     {
  77.                     }
  78.                 }
  79.                 Application.Run(new mainForm(""));
  80.             }
  81.         }
  82.     }
  83. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值