DOBON.NET プログラミング道: .NET Framework, VB.NET, C#, Visual Basic, Visual Studio, インストーラ, ...

ショートカットを作成する

Windows Script Hostを使用する

Windows Script Host(WSH)を使用して簡単にショートカットを作成することができます。それには、まず「Windows Script Host Object Model」(wshom.ocx)を参照設定に追加します。参照設定に追加する方法は、「「○○○.dllを参照に追加します」の意味は?」をご覧ください。Windows Script Host Object Modelは、「参照の追加」ダイアログのCOMタブにあります。

WSHのWshShortcutオブジェクトを使用してショートカットを作成するコードは、以下のようなものです。ここでは、実行しているアプリケーションのショートカットをデスクトップに作成しています。この例では様々なオプションを設定していますが、デフォルトでよければ、ほとんどのオプションは設定する必要がありません。

VB.NET
コードを隠すコードを選択
'作成するショートカットのパス 
Dim shortcutPath As String = System.IO.Path.Combine( _
    Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), _
    "MyApp.lnk")
'ショートカットのリンク先 
Dim targetPath As String = Application.ExecutablePath

'WshShellを作成 
Dim shell As New IWshRuntimeLibrary.WshShellClass()
'ショートカットのパスを指定して、WshShortcutを作成 
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"
'実行時の大きさ 1が通常、3が最大化、7が最小化 
shortcut.WindowStyle = 1
'コメント 
shortcut.Description = "テストのアプリケーション"
'アイコンのパス 自分のEXEファイルのインデックス0のアイコン 
shortcut.IconLocation = Application.ExecutablePath + ",0"

'ショートカットを作成 
shortcut.Save()

'後始末
System.Runtime.InteropServices.Marshal.ReleaseComObject(shortcut)
C#
コードを隠すコードを選択
//作成するショートカットのパス
string shortcutPath = System.IO.Path.Combine(
    Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory),
    @"MyApp.lnk");
//ショートカットのリンク先
string targetPath = Application.ExecutablePath;


//WshShellを作成
IWshRuntimeLibrary.WshShellClass shell = new IWshRuntimeLibrary.WshShellClass();
//ショートカットのパスを指定して、WshShortcutを作成
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";
//実行時の大きさ 1が通常、3が最大化、7が最小化
shortcut.WindowStyle = 1;
//コメント
shortcut.Description = "テストのアプリケーション";
//アイコンのパス 自分のEXEファイルのインデックス0のアイコン
shortcut.IconLocation = Application.ExecutablePath + ",0";

//ショートカットを作成
shortcut.Save();

//後始末
System.Runtime.InteropServices.Marshal.ReleaseComObject(shortcut);

この方法で作成できるショートカットはファイルだけで、フォルダのショートカットは作成できないようです。

COM InteropでShellLinkを使用する方法が、「vbAccelerator - Creating and Modifying Shortcuts」で紹介されています。ここで公開されているShellLinkクラスを使用することで、ショートカットを作成できます。

URLショートカットを作成する

URLショートカットもWindows Script Hostで作成できますが(WshShortcutの代わりにWshUrlShortcutオブジェクトを使います)、URLショートカットはテキストファイルのため、わざわざそうする必要もありません。

以下に、DOBON.NETへのリンクをデスクトップに作成する例を示します。

VB.NET
コードを隠すコードを選択
'作成するURLショートカットのパス 
Dim shortcutPath As String = System.IO.Path.Combine( _
    Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), _
    "DOBON.NET.url")
'ショートカットのリンク先 
Dim targetUrl As String = "http://dobon.net/index.html"

'テキストファイルに書き込む 
Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("shift_jis")
Dim sw As New System.IO.StreamWriter(shortcutPath, False, enc)
sw.WriteLine("[InternetShortcut]")
sw.WriteLine("URL=" & targetUrl)
sw.Close()
C#
コードを隠すコードを選択
//作成するURLショートカットのパス
string shortcutPath = System.IO.Path.Combine(
    Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory),
    @"DOBON.NET.url");
//ショートカットのリンク先
string targetUrl = "http://dobon.net/index.html";

//テキストファイルに書き込む
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift_jis");
System.IO.StreamWriter sw = new System.IO.StreamWriter(shortcutPath, false, enc);
sw.WriteLine("[InternetShortcut]");
sw.WriteLine("URL=" + targetUrl);
sw.Close();

実はこの方法で実行ファイルへのショートカットを作成することもできます。ただしこの場合は、Webブラウザが実行ファイルを開きますので、「ファイルのダウンロード」ダイアログが表示され、「実行」ボタンを押さなければ実行されません。

以下に実行しているアプリケーションのURLショートカットをデスクトップに作成する例を示します。

VB.NET
コードを隠すコードを選択
Dim shortcutPath As String = System.IO.Path.Combine( _
    Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), _
    "MyApp.url")
'ショートカットのリンク先 
Dim targetPath As String = Application.ExecutablePath

'テキストファイルに書き込む 
Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("shift_jis")
Dim sw As New System.IO.StreamWriter(shortcutPath, False, enc)
sw.WriteLine("[InternetShortcut]")
sw.WriteLine("URL=file:///" & targetPath)
'アイコンを指定する 
sw.WriteLine("IconFile=" & targetPath)
sw.WriteLine("IconIndex=0")
sw.Close()
C#
コードを隠すコードを選択
//作成するURLショートカットのパス
string shortcutPath = System.IO.Path.Combine(
    Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory),
    @"MyApp.url");
//ショートカットのリンク先
string targetPath = Application.ExecutablePath;

//テキストファイルに書き込む
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift_jis");
System.IO.StreamWriter sw = new System.IO.StreamWriter(shortcutPath, false, enc);
sw.WriteLine("[InternetShortcut]");
sw.WriteLine("URL=file:///" + targetPath);
//アイコンを指定する
sw.WriteLine("IconFile=" + targetPath);
sw.WriteLine("IconIndex=0");
sw.Close();

注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。

  • このサイトで紹介されているコードの多くは、例外処理が省略されています。例外処理については、こちらをご覧ください。
  • 「???を参照に追加します」の意味が分からないという方は、こちらをご覧ください。