2012-10-26 13 views
7

: C:\TEMP\ में
Get target of shortcut folderमुझे फ़ाइल शॉर्टकट से पथ का नाम कैसे प्राप्त होगा? अपवाद संभव डुप्लिकेट हो रही

उदाहरण के लिए, मैं एक शॉर्टकट test.dll कहा जाता है शॉर्टकट फ़ाइल नाम test.dll

मैं प्राप्त करना चाहते हैं के लिए नेतृत्व करेंगे शॉर्टकट से केवल पथ का नाम स्वयं फ़ाइल में। तो, मैं इस फ़ंक्शन को एक और रिकर्सिव फ़ंक्शन में कॉल कर रहा हूं और इस हार्ड फ़ंक्शन में प्रत्येक बार एक और निर्देशिका डालता हूं।

उदाहरण के लिए, पहली निर्देशिका C:\TEMP है तो C:\TEMP में शॉर्टकट फ़ाइल है जिसे मैं केवल फ़ाइल में पथ प्राप्त करना चाहता हूं। Shortcut (C:\TEMP\hpwmdl23.dat)

तो, मैं क्या प्राप्त करना चाहते है -
hpwmdl23.dat

hpwins23.dat
hpwmdl23.dat: परीक्षण के लिए C:\TEMP में मैं अब 3 फ़ाइलें \ 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
StackTrace:
Shell32.FolderItem.get_GetLink()
पर GatherLinks.Form1.GetShortcutTargetFile(String shortcutFilename) पर
D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs में: line904
GatherLinks.Form1.offlinecrawling

+0

इस उपयोगकर्ता सबसे अधिक संभावना एक प्रतीकात्मक कड़ी (नहीं .lnk में के रूप में एक शॉर्टकट), जिस स्थिति में इस कोड को असफल हो जायेगी हल करने पूछ रहा है। आपको PInvoke के साथ 'GetFinalPathNameByHandle()' का उपयोग करने की आवश्यकता है। इस कोड में नमूना कोड पाया जा सकता है [http://chrisbensen.blogspot.com/2010/06/getfinalpathnamebyhandle.html)। – ykay

उत्तर

18

पर एक शॉर्टकट (.lnk फाइल एक्सटेंशन) का लक्ष्य प्राप्त करने के लिए आप मैं एल निम्नलिखित COM वस्तु है करने के लिए पहली जरूरत है: Windows Script Host Object Model

उसके बाद, आप एक शॉर्टकट

का लक्ष्य प्राप्त करने के लिए उदाहरण

  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 जोड़ने के लिए के तहत समाधान एक्सप्लोरर निम्नलिखित चरणों का प्रयास कर सकते हैं, अपने प्रोजेक्ट के नाम पर राइट क्लिक करें और संदर्भ
  • जोड़ें का चयन करें से टैब कॉम का चयन करें पॉप-अप विंडो
  • के तहत घटक का नाम, का चयन करें विंडोज स्क्रिप्ट होस्ट ऑब्जेक्ट मॉडल
  • पर क्लिक करें ठीक
+0

मैंने निम्नलिखित स्थान से lnk-शॉर्टकट पढ़ने की कोशिश की: सी: \ ProgramData \ Microsoft \ Windows \ प्रारंभ मेनू \ प्रोग्राम \ स्टार्टअप \ shortcut.lnk और मुझे COMException मिलती है: HRESULT: 0x80020009 (DISP_E_EXCEPTION) से। मेरा ओएस: विन 8.1 x64 –

संबंधित मुद्दे