ファイルやフォルダーのショートカットを作成する
プログラムでファイルやフォルダーのショートカットを作成するには次のようにします。
C#の例ではdynamicを使用しているため、C# 4.0 + .NET Framework 4.0以上でないと動作しません。これはVisual Studio 2010相当です。
Type comType = Type.GetTypeFromProgID("WScript.Shell");
dynamic oShell = Activator.CreateInstance(comType);
dynamic oShortcut = oShell.CreateShortcut(@"C:\test\電卓.lnk");
oShortcut.Description = "説明(省略可能)";
oShortcut.TargetPath = @"C:\Windows\System32\calc.exe";
oShortcut.Save();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oShortcut);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oShell);
■リスト1:C#版 ショートカットを作成するDim oShell As Object = CreateObject("WScript.Shell")
Dim oShortcut As Object = oShell.CreateShortcut("C:\test\電卓.lnk")
oShortcut.Description = "説明(省略可能)"
oShortcut.TargetPath = "C:\Windows\System32\calc.exe"
oShortcut.Save()
Runtime.InteropServices.Marshal.ReleaseComObject(oShortcut)
Runtime.InteropServices.Marshal.ReleaseComObject(oShell)
■リスト2:VB版 ショートカットを作成する