You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.8 KiB

  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.InteropServices;
  4. namespace IPA
  5. {
  6. public static class Shortcut
  7. {
  8. private static readonly Type MType = Type.GetTypeFromProgID("WScript.Shell");
  9. private static readonly object MShell = Activator.CreateInstance(MType);
  10. [ComImport, TypeLibType(0x1040), Guid("F935DC23-1CF0-11D0-ADB9-00C04FD58A0B")]
  11. private interface IWshShortcut
  12. {
  13. [DispId(0)]
  14. string FullName { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0)] get; }
  15. [DispId(0x3e8)]
  16. string Arguments { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] set; }
  17. [DispId(0x3e9)]
  18. string Description { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] set; }
  19. [DispId(0x3ea)]
  20. string Hotkey { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] set; }
  21. [DispId(0x3eb)]
  22. string IconLocation { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] set; }
  23. [DispId(0x3ec)]
  24. string RelativePath { [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ec)] set; }
  25. [DispId(0x3ed)]
  26. string TargetPath { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] set; }
  27. [DispId(0x3ee)]
  28. int WindowStyle { [DispId(0x3ee)] get; [param: In] [DispId(0x3ee)] set; }
  29. [DispId(0x3ef)]
  30. string WorkingDirectory { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] set; }
  31. [TypeLibFunc(0x40), DispId(0x7d0)]
  32. void Load([In, MarshalAs(UnmanagedType.BStr)] string pathLink);
  33. [DispId(0x7d1)]
  34. void Save();
  35. }
  36. public static void Create(string fileName, string targetPath, string arguments, string workingDirectory, string description, string hotkey, string iconPath)
  37. {
  38. IWshShortcut shortcut = (IWshShortcut)MType.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, MShell, new object[] { fileName });
  39. shortcut.Description = description;
  40. shortcut.Hotkey = hotkey;
  41. shortcut.TargetPath = targetPath;
  42. shortcut.WorkingDirectory = workingDirectory;
  43. shortcut.Arguments = arguments;
  44. if (!string.IsNullOrEmpty(iconPath))
  45. shortcut.IconLocation = iconPath;
  46. shortcut.Save();
  47. }
  48. }
  49. }