2012-10-22 13 views
7

長い話をするには、C#を使ってフォルダへのショートカットを作成する必要があります。私はIWshRuntimeLibraryを使って読んでいます。 IWshRuntimeLibraryを使って何かしようとすると、私はSystem.IO.Fileであらゆる種類のあいまいなエラーが出ます。私はこれがIWshRuntimeLibrary.FileのインタフェースとSystem.IO.Fileのインタフェースがあるからだと仮定しています。私が本当に見つけたのは、フォルダではなく、アプリへのショートカットを作るための記事です。余談Cのフォルダへのショートカットを作成する#

あいまいエラー:

  • が、これはフォルダのショートカットに使用する適切なツールですか?私はショートカットがまた

を配置した場合、私は、フォルダへのショートカットを作成しようとすると、私が指定する方法この

  • を使用してショートカットを作成するにはどうすればよい
  • は、この使用してC:\TEMPを言う:

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

    COMExceptionとなります。私が読んだことによると、これはCドライブの一時フォルダへのショートカットを作成し、ショートカットをDocuments and Settingsに置くべきです。

  • +0

    可能な重複:http://stackoverflow.com/questions/3391923/placing-a-shortcut-in-users-

    次のスニペットは、デスクトップ上のネットワークフォルダへのショートカットを作成します。起動時のフォルダからウィンドウの開始まで –

    +0

    あいまいなエラーを取り除くには、.NETファイルクラスを使用するときはSystem.IO.Fileを呼び出し、もう一方を使用するときはIWShRuntimeLibrary.Fileを使用する。 System.Windows.FormsとSystem.Threadingの両方を使用するクラスでタイマーを使用する場合は、両方ともTimerクラスが含まれているため、この問題は常に発生します。申し訳ありませんがあなたの主な問題を助けることはできません。 –

    +0

    @ ademing2私はそれが私がやらなければならないことだと思っていましたが、System.IO.Fileメソッド呼び出しの前にそれを追加する前に、正しい軌道に乗っているかどうかを見たいと思っていました。おかげで – swabs

    答えて

    7

    Interop.IWshRuntimeLibraryを参照するには、Interopの種類を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

    +1

    まさに私が探していたもので、私はこの解決策にも近づいていました。ちょうど私の.lnkのショートカットファイルの名前を正しく指定していない – swabs

    1

    専門家交換にIShellLinkを使用しているソリューション:

    Creating Shortcuts in .NET

    あなたはファイルとフォルダshorcutsを作成するための管理層を提供します。

    これはVB.NETですが、フリーコンバーターを使用してC#に変換できます。

    3

    あなたは他の方法の周りにそれを得た:あなたは、あなたのC:\Documents and SettingsフォルダにC:\Tempのショートカットを作成しようとしています。

    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(); 
    
    +1

    私はそれが永遠になっていることに気づいていますが、私は2.7.1のような名前のフォルダへのリンクを作成するためにこのメソッドを使用しようとしています。 Windowsはファイルのようにフォルダを開こうとします。 Windowsがこれをフォルダのショートカットと認識しているかどうかを確認する方法についての考えはありますか? –

    関連する問題