- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using Microsoft.Win32;
- namespace AppC
- {
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main(string[] args)
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- /// 是否通过关联打开的软件
- if (args.Length > 0)
- Application.Run(new mainForm(args[0].ToString()));
- else
- {
- string keyName;
- string keyValue;
- keyName = "WPCFile";
- keyValue = "资源包文件";
- RegistryKey isExCommand = null;
- bool isCreateRegistry = true;
- try
- {
- /// 检查 文件关联是否创建
- isExCommand = Registry.ClassesRoot.OpenSubKey(keyName);
- if (isExCommand == null)
- {
- isCreateRegistry = true;
- }
- else
- {
- if (isExCommand.GetValue("Create").ToString() == Application.ExecutablePath.ToString())
- {
- isCreateRegistry = false;
- }
- else
- {
- Registry.ClassesRoot.DeleteSubKeyTree(keyName);
- isCreateRegistry = true;
- }
- }
- }
- catch (Exception)
- {
- isCreateRegistry = true;
- }
- /// 假如 文件关联 还没有创建,或是关联位置已被改变
- if (isCreateRegistry)
- {
- try
- {
- RegistryKey key, keyico;
- key = Registry.ClassesRoot.CreateSubKey(keyName);
- key.SetValue("Create", Application.ExecutablePath.ToString());
- keyico = key.CreateSubKey("DefaultIcon");
- keyico.SetValue("", Application.ExecutablePath + ",0");
- key.SetValue("", keyValue);
- key = key.CreateSubKey("Shell");
- key = key.CreateSubKey("Open");
- key = key.CreateSubKey("Command");
- /// 关联的位置
- key.SetValue("", "/"" + Application.ExecutablePath.ToString() + "/" /"%1/"");
- /// 关联的文件扩展名,
- keyName = ".wpc";
- keyValue = "WPCFile";
- key = Registry.ClassesRoot.CreateSubKey(keyName);
- key.SetValue("", keyValue);
- }
- catch (Exception)
- {
- }
- }
- Application.Run(new mainForm(""));
- }
- }
- }
- }
使用C#在程序运行时创建文件关联
最新推荐文章于 2020-09-03 03:33:25 发布