OpenHelper.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Ui.Common.Helper
  7. {
  8. public static partial class OpenHelper
  9. {
  10. [LibraryImport("shell32.dll", SetLastError = true)]
  11. public static partial int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, IntPtr apidl, uint dwFlags);
  12. [LibraryImport("shell32.dll", SetLastError = true)]
  13. public static partial void ILFree(IntPtr pidlList);
  14. [LibraryImport("shell32.dll", SetLastError = true)]
  15. public static partial IntPtr ILCreateFromPathW([MarshalAs(UnmanagedType.LPWStr)] string pszPath);
  16. public static void OpenFolder(string path)
  17. {
  18. if (Directory.Exists(path))
  19. {
  20. Process.Start(new ProcessStartInfo
  21. {
  22. FileName = path,
  23. UseShellExecute = true,
  24. Verb = "open"
  25. });
  26. }
  27. else
  28. {
  29. Logger.Notice.Print(LogClass.Application, $"Directory \"{path}\" doesn't exist!");
  30. }
  31. }
  32. public static void LocateFile(string path)
  33. {
  34. if (File.Exists(path))
  35. {
  36. if (OperatingSystem.IsWindows())
  37. {
  38. IntPtr pidlList = ILCreateFromPathW(path);
  39. if (pidlList != IntPtr.Zero)
  40. {
  41. try
  42. {
  43. Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
  44. }
  45. finally
  46. {
  47. ILFree(pidlList);
  48. }
  49. }
  50. }
  51. else if (OperatingSystem.IsMacOS())
  52. {
  53. Process.Start("open", $"-R \"{path}\"");
  54. }
  55. else if (OperatingSystem.IsLinux())
  56. {
  57. Process.Start("dbus-send", $"--session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:\"file://{path}\" string:\"\"");
  58. }
  59. else
  60. {
  61. OpenFolder(Path.GetDirectoryName(path));
  62. }
  63. }
  64. else
  65. {
  66. Logger.Notice.Print(LogClass.Application, $"File \"{path}\" doesn't exist!");
  67. }
  68. }
  69. public static void OpenUrl(string url)
  70. {
  71. if (OperatingSystem.IsWindows())
  72. {
  73. Process.Start(new ProcessStartInfo("cmd", $"/c start {url.Replace("&", "^&")}"));
  74. }
  75. else if (OperatingSystem.IsLinux())
  76. {
  77. Process.Start("xdg-open", url);
  78. }
  79. else if (OperatingSystem.IsMacOS())
  80. {
  81. Process.Start("open", url);
  82. }
  83. else
  84. {
  85. Logger.Notice.Print(LogClass.Application, $"Cannot open url \"{url}\" on this platform!");
  86. }
  87. }
  88. }
  89. }