using BPA.Helper; using BPASmart.Model; using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BPASmart.ConfigurationSoftware { public class FileHelper { private volatile static FileHelper _Instance; public static FileHelper GetInstance => _Instance ?? (_Instance = new FileHelper()); private FileHelper() { } public void RegisterOpenFileType() { string icoFile = System.Windows.Forms.Application.StartupPath + $"\\fyf.ico"; string DirectoryPath = $"{Json.Data.ProjectPath}\\Images"; Directory.CreateDirectory(DirectoryPath); File.Copy(icoFile, $"{DirectoryPath}\\fyf.ico"); RegisterFileType(".project", "HBL", ".project", System.Windows.Forms.Application.ExecutablePath, $"{DirectoryPath}\\fyf.ico"); } private void RegisterFileType(string typeName, string fileType, string fileContent, string app, string ico) { string toolPath = app;//工具启动路径 string extension = typeName;//fileType = "自定义文件类型"; //fileContent = "AAAA"; //获取信息 RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension); if (registryKey != null) { try { RegistryKey _Regkey = Registry.ClassesRoot.OpenSubKey("", true); RegistryKey _VRPkey = _Regkey.OpenSubKey(extension); if (_VRPkey != null) _Regkey.DeleteSubKeyTree(extension, true); if (_VRPkey != null) _Regkey.DeleteSubKeyTree("Exec"); } catch (Exception e) { } } if (registryKey != null && registryKey.OpenSubKey("shell") != null && registryKey.OpenSubKey("shell").OpenSubKey("open") != null && registryKey.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command") != null) { var varSub = registryKey.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command"); var varValue = varSub.GetValue(""); if (Equals(varValue, toolPath + " \"%1\"")) { return; } } //文件注册 registryKey = Registry.ClassesRoot.CreateSubKey(extension); registryKey.SetValue("", fileType); registryKey.SetValue("Content Type", fileContent); //设置默认图标 RegistryKey iconKey = registryKey.CreateSubKey("DefaultIcon"); //iconKey.SetValue("", Application.StartupPath + $"\\{ico}.ico"); iconKey.SetValue("", ico); iconKey.Close(); //设置默认打开程序路径 registryKey = registryKey.CreateSubKey("shell\\open\\command"); registryKey.SetValue("", toolPath + " \"%1\""); //关闭 registryKey.Close(); SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero); } [DllImport("shell32.dll")] public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); } }