以下代码做个Mark

/// <summary>
/// Create an associaten for a file extension in the windows registry
/// CreateAssociation(@"vendor.application",".tmf","Tool file",@"C:\Windows\SYSWOW64\notepad.exe",@"%SystemRoot%\SYSWOW64\notepad.exe,0");
/// </summary>
/// <param name="ProgID">e.g. vendor.application</param>
/// <param name="extension">e.g. .tmf</param>
/// <param name="description">e.g. Tool file</param>
/// <param name="application">e.g. @"C:\Windows\SYSWOW64\notepad.exe"</param>
/// <param name="icon">@"%SystemRoot%\SYSWOW64\notepad.exe,0"</param>
/// <param name="hive">e.g. The user-specific settings have priority over the computer settings. KeyHive.LocalMachine need admin rights</param>
public static void CreateAssociation(string ProgID, string extension, string description, string application, string icon, KeyHiveSmall hive = KeyHiveSmall.CurrentUser)
{
RegistryKey selectedKey = null; switch (hive)
{
case KeyHiveSmall.ClassesRoot:
Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension).SetValue("", ProgID);
selectedKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProgID);
break; case KeyHiveSmall.CurrentUser:
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + extension).SetValue("", ProgID);
selectedKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + ProgID);
break; case KeyHiveSmall.LocalMachine:
Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\Classes\" + extension).SetValue("", ProgID);
selectedKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\Classes\" + ProgID);
break;
} if (selectedKey != null)
{
if (description != null)
{
selectedKey.SetValue("", description);
}
if (icon != null)
{
selectedKey.CreateSubKey("DefaultIcon").SetValue("", icon, RegistryValueKind.ExpandString);
selectedKey.CreateSubKey(@"Shell\Open").SetValue("icon", icon, RegistryValueKind.ExpandString);
}
if (application != null)
{
selectedKey.CreateSubKey(@"Shell\Open\command").SetValue("", "\"" + application + "\"" + " \"%1\"", RegistryValueKind.ExpandString);
}
}
selectedKey.Flush();
selectedKey.Close();
}
/// <summary>
/// Creates a association for current running executable
/// </summary>
/// <param name="extension">e.g. .tmf</param>
/// <param name="hive">e.g. KeyHive.LocalMachine need admin rights</param>
/// <param name="description">e.g. Tool file. Displayed in explorer</param>
public static void SelfCreateAssociation(string extension, KeyHiveSmall hive = KeyHiveSmall.CurrentUser, string description = "")
{
string ProgID = System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType.FullName;
string FileLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
CreateAssociation(ProgID, extension, description, FileLocation, FileLocation + ",0", hive);
}

最新文章

  1. c# socket
  2. 异步IO比同步阻塞IO性能更好吗?为什么?
  3. SSH框架总结
  4. java 将list 按长度分割
  5. 【读书笔记】iOS-对象初始化
  6. dede调用文章的栏目,作者,发布时间.以及连接数据库输出一个表
  7. Oracle Enterprise linux 7 安装Oracle11gR2
  8. 常用Latex公式
  9. Redis基础一(Linux)
  10. hashtable 简单介绍
  11. 【转载】IIC SPI UART串行总线
  12. mybatis学习--缓存(一级和二级缓存)
  13. Ubuntu 分辨率显示出错,分辨率不是最佳分辨率的解决办法
  14. java 上传图片压缩图片
  15. 不可恢复的生成错误mergemod.dll 2.0.2600.0
  16. MySQL时间戳时间
  17. python--利用列表推导式快速生成xml格式数据
  18. 值类型 struct
  19. Hangfire JobStorage.Current property value has not been initialized
  20. 简易APB4 slave实践

热门文章

  1. jfinal 导出excle
  2. C++ 消失的析构函数 —— virtual 实现的动态析构
  3. yum install nginx-没有可用软件包 nginx。
  4. celery 原理和组件
  5. 题解洛谷P1538【迎春舞会之数字舞蹈】
  6. DirectX12的初始化
  7. js 点击input焦点不弹出键盘 PDA扫描枪
  8. Core3.0读取appsetting.json中的配置参数
  9. winform使用Barcodex控件预览和打印一维码
  10. .net网站快速停机设置app_offline