10

簡単に言うと、C# を使用してフォルダーへのショートカットを作成する必要があります。を使用して読んでいIWshRuntimeLibraryます。を使用して何かを試してみるとIWshRuntimeLibrary、あらゆる種類のあいまいなエラーが発生しSystem.IO.Fileます。IWshRuntimeLibrary.Fileこれは、と一緒にインターフェースがあるためだと思いますSystem.IO.File。私が実際に見つけたのは、フォルダーではなく、アプリへのショートカットの作成に関する記事だけです。

あいまいなエラーはさておき:

  • これはフォルダ ショートカットに使用する適切なツールですか?
  • これを使用してショートカットを作成するにはどうすればよいですか
  • ショートカットの配置場所を指定する方法

また、フォルダーへのショートカットを作成しようとするときは、次のように言いますC:\TEMP

IWshShortcut shortcut;
wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(@"C:\TEMP");

shortcut.TargetPath = @"C:\Documents and Settings";

を取得しCOMExceptionます。私が読んだことによると、これはCドライブの一時フォルダーへのショートカットを作成し、そのショートカットをドキュメントと設定に配置する必要があります。

4

3 に答える 3

11

Interop.IWshRuntimeLibrary を参照するには、Embed Interop Types を False に設定することを忘れないでください。エラーが発生することなくテストして動作します。

  // Make sure you use try/catch block because your App may has no permissions on the target path!
  try
  {
    CreateShortcut(@"C:\temp", @"C:\MyShortcutFile.lnk",
        "Custom Shortcut", "/param", "Ctrl+F", @"c:\");
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
  }



   /// <summary>
    /// Create Windows Shorcut
    /// </summary>
    /// <param name="SourceFile">A file you want to make shortcut to</param>
    /// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
    public static void CreateShortcut(string SourceFile, string ShortcutFile)
    {
      CreateShortcut(SourceFile, ShortcutFile, null, null, null, null);
    }

    /// <summary>
    /// Create Windows Shorcut
    /// </summary>
    /// <param name="SourceFile">A file you want to make shortcut to</param>
    /// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
    /// <param name="Description">Shortcut description</param>
    /// <param name="Arguments">Command line arguments</param>
    /// <param name="HotKey">Shortcut hot key as a string, for example "Ctrl+F"</param>
    /// <param name="WorkingDirectory">"Start in" shorcut parameter</param>
    public static void CreateShortcut(string TargetPath, string ShortcutFile, string Description,
       string Arguments, string HotKey, string WorkingDirectory)
    {
      // Check necessary parameters first:
      if (String.IsNullOrEmpty(TargetPath))
        throw new ArgumentNullException("TargetPath");
      if (String.IsNullOrEmpty(ShortcutFile))
        throw new ArgumentNullException("ShortcutFile");

      // Create WshShellClass instance:
      var wshShell = new WshShellClass();

      // Create shortcut object:
      IWshRuntimeLibrary.IWshShortcut shorcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(ShortcutFile);

      // Assign shortcut properties:
      shorcut.TargetPath = TargetPath;
      shorcut.Description = Description;
      if (!String.IsNullOrEmpty(Arguments))
        shorcut.Arguments = Arguments;
      if (!String.IsNullOrEmpty(HotKey))
        shorcut.Hotkey = HotKey;
      if (!String.IsNullOrEmpty(WorkingDirectory))
        shorcut.WorkingDirectory = WorkingDirectory;

      // Save the shortcut:
      shorcut.Save();
    }

ソース: http://zayko.net/post/How-to-create-Windows-shortcut-(C).aspx

于 2012-10-22T20:53:08.227 に答える
4

逆に言えば、フォルダC:\Tempへのショートカットを作成しようとしているのです。C:\Documents and Settings

次のスニペットは、デスクトップ上のネットワーク フォルダーへのショートカットを作成します。

var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
IWshShortcut shortcut;
var wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(Path.Combine(desktop, @"Temp.lnk"));
shortcut.TargetPath = @"\\computername\sharename";
shortcut.Save();
于 2012-10-22T21:00:10.820 に答える
-1

IShellLinkエキスパート交換で使用しているソリューション:

.NET でショートカットを作成する

ファイルとフォルダーのショートカットを作成するためのマネージド レイヤーを提供します。

VB.NET ですが、無料のコンバーターを使用して C# に変換できます。

于 2012-10-22T20:42:52.760 に答える