ExplorerInterops.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 
  2. namespace SinMaiLauncher.Interops
  3. {
  4. using System.Diagnostics;
  5. using System.Text;
  6. using Vanara.Extensions;
  7. using Vanara.PInvoke;
  8. using static Vanara.PInvoke.Shell32;
  9. using IServiceProvider = Vanara.PInvoke.Shell32.IServiceProvider;
  10. public static class ExplorerInterops
  11. {
  12. private static readonly Guid SID_STopLevelBrowser = new("4C96BE40-915C-11CF-99D3-00AA004AE837");
  13. internal static ILogger? logger = null;
  14. static ExplorerInterops()
  15. {
  16. Ole32.CoInitializeEx(IntPtr.Zero, Ole32.COINIT.COINIT_APARTMENTTHREADED);
  17. }
  18. public static string[] GetOpenWindows()
  19. {
  20. var shellWindows = new SHDocVw.ShellWindows();
  21. var lst = new List<string>();
  22. foreach (var window in shellWindows)
  23. {
  24. try
  25. {
  26. var svcProvider = (IServiceProvider)window;
  27. var shellBrowser = svcProvider.QueryService<IShellBrowser>(SID_STopLevelBrowser);
  28. IShellView? shellView = null;
  29. shellBrowser?.QueryActiveShellView(out shellView);
  30. object qiFolderView = null;
  31. shellView?.QueryInterface(typeof(IFolderView).GUID, out qiFolderView);
  32. var fv = (IFolderView)qiFolderView;
  33. var objPf = fv?.GetFolder(typeof(IPersistFolder2).GUID);
  34. var pf2 = (IPersistFolder2)objPf;
  35. var pIdl = PIDL.Null;// new("");
  36. pf2?.GetCurFolder(ref pIdl);
  37. var sb = new StringBuilder(260);
  38. if (SHGetPathFromIDList(pIdl, sb))
  39. {
  40. lst.Add(sb.ToString());
  41. }
  42. else
  43. {
  44. //var so1 = SHGetNameFromIDList(pidl, SIGDN.SIGDN_NORMALDISPLAY, out var name);
  45. var so2 = SHGetNameFromIDList(pIdl, SIGDN.SIGDN_DESKTOPABSOLUTEPARSING, out var clsid);
  46. if (so2.Succeeded) lst.Add(clsid);
  47. }
  48. pIdl.Dispose();
  49. }
  50. catch (Exception e)
  51. {
  52. var breakPoint = 0;
  53. }
  54. }
  55. return lst.ToArray();
  56. }
  57. public static void KillExplorer()
  58. {
  59. try
  60. {
  61. Process.Start(new ProcessStartInfo
  62. {
  63. FileName = "taskkill",
  64. Arguments = "/f /im explorer.exe",
  65. CreateNoWindow = true, // 不显示命令行窗口
  66. UseShellExecute = false // 不需要 Shell 执行
  67. })?.WaitForExit();
  68. }
  69. catch (Exception)
  70. {
  71. var breakPoint = 0;
  72. }
  73. }
  74. public static void RestoreExplorer() => Process.Start("explorer.exe");
  75. public static void OpenWindow(IReadOnlyList<string> pathOrClsid)
  76. {
  77. logger?.LogInformation($"Restore explorer total: {pathOrClsid.Count}");
  78. for (var index = 0; index < pathOrClsid.Count; index++)
  79. {
  80. var poc = pathOrClsid[index];
  81. try
  82. {
  83. logger?.LogInformation($"Restore explorer[{index}]: {poc}");
  84. Process.Start(new ProcessStartInfo
  85. {
  86. FileName = "explorer.exe",
  87. Arguments = $"\"{poc}\"", // 支持路径或 CLSID
  88. UseShellExecute = true
  89. });
  90. }
  91. catch (Exception)
  92. {
  93. var breakPoint = 0;
  94. }
  95. }
  96. }
  97. }
  98. }