Advertisement
tolikpunkoff

Create Windows shortcut (LNK) with additional parameters

Oct 12th, 2018
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using IWshRuntimeLibrary;
  2. //...
  3. public enum ShortcutWindowStyle
  4. {
  5.     Normal = 1,
  6.     Maximize = 3,
  7.     Minimize = 7
  8. }
  9. //...
  10. public static void Create(string ShortcutPath, string TargetPath,
  11.             string WorkingDirectory, ShortcutWindowStyle WindowStyle,
  12.             string Arguments, string Icon, string Description,
  13.             string Hotkey)
  14. {
  15.     WshShell wshShell = new WshShell(); //создаем объект wsh shell
  16.  
  17.     IWshShortcut Shortcut = (IWshShortcut)wshShell.
  18.         CreateShortcut(ShortcutPath);
  19.  
  20.     Shortcut.TargetPath = TargetPath; //путь к целевому файлу
  21.  
  22.     //в качестве рабочей директории установим каталог с файлом
  23.     //для которого cоздаем ярлык если рабочий каталог null
  24.     if (WorkingDirectory == null)
  25.     {
  26.         Shortcut.WorkingDirectory =
  27.             System.IO.Path.GetDirectoryName(TargetPath);
  28.     }
  29.     else
  30.     {
  31.         Shortcut.WorkingDirectory = WorkingDirectory;
  32.     }
  33.  
  34.     //стиль окна приложения
  35.     Shortcut.WindowStyle = (int)WindowStyle;
  36.  
  37.     //Параметры командной строки
  38.     Shortcut.Arguments = Arguments;
  39.  
  40.     //Иконка
  41.     if (!string.IsNullOrEmpty(Icon))
  42.     {
  43.         Shortcut.IconLocation = Icon;
  44.     }
  45.  
  46.     //Описание
  47.     Shortcut.Description = Description;
  48.  
  49.     //Горячая клавиша
  50.     if (!string.IsNullOrEmpty(Hotkey))
  51.     {
  52.         Shortcut.Hotkey = Hotkey;
  53.     }
  54.  
  55.    
  56.     Shortcut.Save();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement