■■■

2016年4月3日日曜日

ショートカットを動的に作成する方法

ショートカットを動的に作成する方法
ショートカットを動的に作成する際にはIWshRuntimeLibrary.WshShellを利用します。
VB.NET

Dim shortcutPath As String = System.IO.Path.Combine( _
Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), _
"MyApp.lnk")

Dim targetPath As String = Application.ExecutablePath


Dim shell As New IWshRuntimeLibrary.WshShell()

Dim shortcut As IWshRuntimeLibrary.IWshShortcut = _
DirectCast(shell.CreateShortcut(shortcutPath), _
IWshRuntimeLibrary.IWshShortcut)

shortcut.TargetPath = targetPath

shortcut.Arguments = "/a /b /c"

shortcut.WorkingDirectory = Application.StartupPath

shortcut.Hotkey = "Ctrl+Alt+Shift+F12"

shortcut.WindowStyle = 1

shortcut.Description = "ショートカット作成"

shortcut.IconLocation = Application.ExecutablePath + ",0"


shortcut.Save()

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell)
C#

string shortcutPath = System.IO.Path.Combine(
Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory),
@"MyApp.lnk");

string targetPath = Application.ExecutablePath;

IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();

IWshRuntimeLibrary.IWshShortcut shortcut =
(IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);

shortcut.TargetPath = targetPath;

shortcut.Arguments = "/a /b /c";

shortcut.WorkingDirectory = Application.StartupPath;

shortcut.Hotkey = "Ctrl+Alt+Shift+F12";

shortcut.WindowStyle = 1;

shortcut.Description = "ショートカット作成";

shortcut.IconLocation = Application.ExecutablePath + ",0";


shortcut.Save();


System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell);
■■■