123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Extensometer.BLL
- {
- public class FileTypeRegInfo
- {
- /// <summary>
- /// 目标类型文件的拓展名
- /// </summary>
- public string ExtendName;
- /// <summary>
- /// 目标文件类型说明
- /// </summary>
- public string Description;
- /// <summary>
- /// 目标类型文件关联的图标
- /// </summary>
- public string IcoPath;
- /// <summary>
- /// 打开目标类型文件的应用程序
- /// </summary>
- public string ExePath;
- public FileTypeRegInfo() { }
- public FileTypeRegInfo(string extendName)
- {
- this.ExtendName = extendName;
- }
- }
- public class FileTypeRegisterClass
- {
- /// <summary>
- /// 创建单例模型
- /// </summary>
- private static FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass();
- private FileTypeRegisterClass()
- { }
- public static FileTypeRegisterClass GetFileTypeRegister()
- {
- return fileTypeRegister;
- }
- /// <summary>
- /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。
- /// </summary>
- private static void RegisterFileType(FileTypeRegInfo regInfo)
- {
- if (FileTypeRegistered(regInfo.ExtendName))
- {
- return;
- }
- string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType";
- RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName);
- fileTypeKey.SetValue("", relationName);
- fileTypeKey.Close();
- RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName);
- relationKey.SetValue("", regInfo.Description);
- RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");
- iconKey.SetValue("", regInfo.IcoPath);
- RegistryKey shellKey = relationKey.CreateSubKey("Shell");
- RegistryKey openKey = shellKey.CreateSubKey("Open");
- RegistryKey commandKey = openKey.CreateSubKey("Command");
- commandKey.SetValue("", regInfo.ExePath + " %1");
- relationKey.Close();
- }
- /// <summary>
- /// GetFileTypeRegInfo 得到指定文件类型关联信息
- /// </summary>
- private static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
- {
- if (!FileTypeRegistered(extendName))
- {
- return null;
- }
- FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName);
- string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
- RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName);
- regInfo.Description = relationKey.GetValue("").ToString();
- RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon");
- regInfo.IcoPath = iconKey.GetValue("").ToString();
- RegistryKey shellKey = relationKey.OpenSubKey("Shell");
- RegistryKey openKey = shellKey.OpenSubKey("Open");
- RegistryKey commandKey = openKey.OpenSubKey("Command");
- string temp = commandKey.GetValue("").ToString();
- regInfo.ExePath = temp.Substring(0, temp.Length - 3);
- return regInfo;
- }
- /// <summary>
- /// UpdateFileTypeRegInfo 更新指定文件类型关联信息
- /// </summary>
- private static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
- {
- if (!FileTypeRegistered(regInfo.ExtendName))
- {
- return false;
- }
- string extendName = regInfo.ExtendName;
- string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
- RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true);
- relationKey.SetValue("", regInfo.Description);
- RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true);
- iconKey.SetValue("", regInfo.IcoPath);
- RegistryKey shellKey = relationKey.OpenSubKey("Shell");
- RegistryKey openKey = shellKey.OpenSubKey("Open");
- RegistryKey commandKey = openKey.OpenSubKey("Command", true);
- commandKey.SetValue("", regInfo.ExePath + " %1");
- relationKey.Close();
- return true;
- }
- /// <summary>
- /// FileTypeRegistered 指定文件类型是否已经注册
- /// </summary>
- private static bool FileTypeRegistered(string extendName)
- {
- RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
- if (softwareKey != null)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 检查并创建项目类型
- /// </summary>
- public void CheckAndCreateFileType()
- {
- if (!FileTypeRegisterClass.FileTypeRegistered(".Esm")) //如果文件类型没有注册,则进行注册
- {
- FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".Esm"); //文件类型信息
- fileTypeRegInfo.Description = "引伸计工程文件";
- fileTypeRegInfo.ExePath = Application.ExecutablePath;
- fileTypeRegInfo.ExtendName = ".Esm";
- fileTypeRegInfo.IcoPath = Application.ExecutablePath; //文件图标使用应用程序的
- FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass(); //注册
- FileTypeRegisterClass.RegisterFileType(fileTypeRegInfo);
- Process[] process = Process.GetProcesses(); //重启Explorer进程,使更新立即生效
- var p = (from proc in process
- where proc.ProcessName.Equals("explorer")
- select proc).FirstOrDefault();
- p.Kill();
- }
- else
- {
- FileTypeRegInfo fileTypeRegInfo = GetFileTypeRegInfo(".Esm");
- //若项目位置有变,则更新项目信息
- if(fileTypeRegInfo.ExePath != Application.ExecutablePath)
- {
- UpdateFileType();
- }
- }
- }
- /// <summary>
- /// 更新项目类型
- /// </summary>
- public void UpdateFileType()
- {
- FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".Esm"); //文件类型信息
- fileTypeRegInfo.Description = "引伸计工程文件";
- fileTypeRegInfo.ExePath = Application.ExecutablePath;
- fileTypeRegInfo.ExtendName = ".Esm";
- fileTypeRegInfo.IcoPath = Application.ExecutablePath; //文件图标使用应用程序的
- FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass(); //注册
- FileTypeRegisterClass.UpdateFileTypeRegInfo(fileTypeRegInfo);
- Process[] process = Process.GetProcesses(); //重启Explorer进程,使更新立即生效
- var p = (from proc in process
- where proc.ProcessName.Equals("explorer")
- select proc).FirstOrDefault();
- p.Kill();
- }
- /// <summary>
- /// 文件路径转所在文件夹路径
- /// </summary>
- /// <param name="FilePath"></param>
- /// <returns></returns>
- public string FilePathToFlodePath(string FilePath)
- {
- string result = string.Empty;
- string[] results = FilePath.Split('\\');
- for (int i = 0; i < results.Count() - 1; i++)
- {
- if (i == 0)
- {
- result = results[0];
- }
- else
- {
- result = result + "\\" + results[i];
- }
- }
- return result;
- }
- }
- }
|