Common.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace AIRS
  8. {
  9. /// <summary>
  10. /// function:Common
  11. /// author :lbf
  12. /// date :2020/6/16 13:21:21
  13. /// </summary>
  14. public class Common
  15. {
  16. public enum ShowCommands : int
  17. {
  18. SW_HIDE = 0,
  19. SW_SHOWNORMAL = 1,
  20. SW_NORMAL = 1,
  21. SW_SHOWMINIMIZED = 2,
  22. SW_SHOWMAXIMIZED = 3,
  23. SW_MAXIMIZE = 3,
  24. SW_SHOWNOACTIVATE = 4,
  25. SW_SHOW = 5,
  26. SW_MINIMIZE = 6,
  27. SW_SHOWMINNOACTIVE = 7,
  28. SW_SHOWNA = 8,
  29. SW_RESTORE = 9,
  30. SW_SHOWDEFAULT = 10,
  31. SW_FORCEMINIMIZE = 11,
  32. SW_MAX = 11
  33. }
  34. [DllImport("shell32.dll", ExactSpelling = true)]
  35. private static extern void ILFree(IntPtr pidlList);
  36. [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
  37. private static extern IntPtr ILCreateFromPathW(string pszPath);
  38. [DllImport("shell32.dll", ExactSpelling = true)]
  39. private static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags);
  40. [DllImport("shell32.dll")]
  41. static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
  42. public static void locateFile(string filePath)
  43. {
  44. IntPtr pidlList = ILCreateFromPathW(filePath);
  45. if (pidlList != IntPtr.Zero)
  46. {
  47. try
  48. {
  49. Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
  50. }
  51. finally
  52. {
  53. ILFree(pidlList);
  54. }
  55. }
  56. else
  57. {
  58. ShellExecute(IntPtr.Zero, "open", "explorer.exe", "/select,\"" + filePath, "\"", ShowCommands.SW_SHOWNORMAL);
  59. }
  60. }
  61. public static string removeControlChars(string str)
  62. {
  63. return new string(str.Where(s => s >= 32 && s != 127 && (s < 0x80 || s > 0x9F)).ToArray());//路径中有控制字符,进行过滤 lbf 2020/10/28
  64. }
  65. }
  66. }