终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FileHelper.cs 3.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using BPA.Helper;
  2. using BPASmart.Model;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace BPASmart.ConfigurationSoftware
  13. {
  14. public class FileHelper
  15. {
  16. private volatile static FileHelper _Instance;
  17. public static FileHelper GetInstance => _Instance ?? (_Instance = new FileHelper());
  18. private FileHelper() { }
  19. public void RegisterOpenFileType()
  20. {
  21. string icoFile = System.Windows.Forms.Application.StartupPath + $"\\fyf.ico";
  22. string DirectoryPath = $"{Json<ProjectModel>.Data.ProjectPath}\\Images";
  23. Directory.CreateDirectory(DirectoryPath);
  24. File.Copy(icoFile, $"{DirectoryPath}\\fyf.ico");
  25. RegisterFileType(".project", "HBL", ".project", System.Windows.Forms.Application.ExecutablePath, $"{DirectoryPath}\\fyf.ico");
  26. }
  27. private void RegisterFileType(string typeName, string fileType, string fileContent, string app, string ico)
  28. {
  29. string toolPath = app;//工具启动路径
  30. string extension = typeName;//fileType = "自定义文件类型";
  31. //fileContent = "AAAA";
  32. //获取信息
  33. RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension);
  34. if (registryKey != null)
  35. {
  36. try
  37. {
  38. RegistryKey _Regkey = Registry.ClassesRoot.OpenSubKey("", true);
  39. RegistryKey _VRPkey = _Regkey.OpenSubKey(extension);
  40. if (_VRPkey != null) _Regkey.DeleteSubKeyTree(extension, true);
  41. if (_VRPkey != null) _Regkey.DeleteSubKeyTree("Exec");
  42. }
  43. catch (Exception e)
  44. {
  45. }
  46. }
  47. if (registryKey != null && registryKey.OpenSubKey("shell") != null && registryKey.OpenSubKey("shell").OpenSubKey("open") != null &&
  48. registryKey.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command") != null)
  49. {
  50. var varSub = registryKey.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command");
  51. var varValue = varSub.GetValue("");
  52. if (Equals(varValue, toolPath + " \"%1\""))
  53. {
  54. return;
  55. }
  56. }
  57. //文件注册
  58. registryKey = Registry.ClassesRoot.CreateSubKey(extension);
  59. registryKey.SetValue("", fileType);
  60. registryKey.SetValue("Content Type", fileContent);
  61. //设置默认图标
  62. RegistryKey iconKey = registryKey.CreateSubKey("DefaultIcon");
  63. //iconKey.SetValue("", Application.StartupPath + $"\\{ico}.ico");
  64. iconKey.SetValue("", ico);
  65. iconKey.Close();
  66. //设置默认打开程序路径
  67. registryKey = registryKey.CreateSubKey("shell\\open\\command");
  68. registryKey.SetValue("", toolPath + " \"%1\"");
  69. //关闭
  70. registryKey.Close();
  71. SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
  72. }
  73. [DllImport("shell32.dll")]
  74. public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
  75. }
  76. }