2012-10-26 23 views
7

例外可能性の重複取得:C:\TEMP\に例えば
Get target of shortcut folderファイルのショートカットからパス名を取得するにはどうすればよいですか?

を、私はショートカットがtest.dll

は私が取得したいファイル名につながるtest.dllと呼ばれるショートカットを持っていますショートカットからファイルへのパス名だけを自己作成します。 私は別の再帰関数でこの関数を呼び出して、この関数をマイコンピュータの別のディレクトリに置くつもりです。

たとえば、最初のディレクトリはC:\TEMPで、C:\TEMPにはファイルへのパスのみを取得するショートカットファイルがあります。 C:\TEMPではテストのために、私は今、3つのファイルがあります。

hpwins23.dat
hpwmdl23.dat
hpwmdl23.dat - ShortcutC:\TEMP\hpwmdl23.dat

をだから、私が取得したいことはありますこの場合、ショートカットのCのパス名:\ TEMP

私はこの機能を使用しようとしました:

public string GetShortcutTargetFile(string shortcutFilename) 
     { 
      string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); 
      string filenameOnly = System.IO.Path.GetFileName(shortcutFilename); 
      Shell shell = new Shell(); 
      Folder folder = shell.NameSpace(pathOnly); 
      if (folder == null) 
      { 
      } 
      else 
      { 
       FolderItem folderItem = folder.ParseName(filenameOnly); 
       if (folderItem != null) 
       { 
        Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; 
        return link.Path; 
       } 
      } 
      return string.Empty; 
     } 

が、私は機能を使用していますし、その私はライン上で例外エラーを取得していますショートカットになったとき:

Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink //The exception is: NotImplementedException: The method or operation is not implemented 

私はそれを解決するために行うshoud?

これは、完全な例外エラーメッセージである:

System.NotImplementedException
引っ掛かったメッセージ =メソッドまたは動作が実装されていません。
ソース = GatherLinks
のStackTraceD:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs
GatherLinks.Form1.GetShortcutTargetFile(String shortcutFilename)Shell32.FolderItem.get_GetLink()

GatherLinks.Form1.offlinecrawling

+0

にこのユーザーが最も可能性が高い。このコードは失敗します。その場合にはシンボリックリンク(.LNKのようではないショートカット)を、解決するために求めています。 PInvokeで 'GetFinalPathNameByHandle()'を使う必要があります。サンプルコードはこの[entry here](http://chrisbensen.blogspot.com/2010/06/getfinalpathnamebyhandle.html)にあります。 – ykay

答えて

18

line904
ショートカット(.lnkファイル拡張子)の目標を取得するにはあなたあなたは

  string linkPathName = @"D:\Picrofo Autobot.lnk"; // Change this to the shortcut path 

      if (System.IO.File.Exists(linkPathName)) 
      { 
      // WshShellClass shell = new WshShellClass(); 
       WshShell shell = new WshShell(); //Create a new WshShell Interface 
       IWshShortcut link = (IWshShortcut)shell.CreateShortcut(linkPathName); //Link the interface to our shortcut 

       MessageBox.Show(link.TargetPath); //Show the target in a MessageBox using IWshShortcut 
      } 

おかげショートカット

の例をターゲットを取得するWshShell(またはWshShellClass)とIWshShortcutインタフェースを使用することができ、その後Windows Script Host Object Model

:lは、以下のCOMオブジェクトを持っている最初の必要があります
この情報がお役に立てば幸いです。


あなたは、ソリューションエクスプローラの下、プロジェクト

  • Windows Script Host Object Modelを追加するには、以下の手順を試して、自分のプロジェクト名を右クリックし、追加リファレンス
  • を選択するから、タブCOMを選択ポップアップウィンドウ
  • コンポーネント名Windowsスクリプトホストオブジェクトモデル
  • クリックOK
+0

私は次の場所からlnk-shortcutを読み込もうとしました:C:\ ProgramData \ Microsoft \ Windows \ Start Menu \ Programs \ StartUp \ shortcut.lnkと私はCOMExceptionを取得します:HRESULTから:0x80020009(DISP_E_EXCEPTION)。私のOS:Win 8.1 x64 –

関連する問題