A few days ago, to do a small program encountered problems, in Baidu search for a long time, did not find a good solution, the English site immediately search methods, it seems that the resources abroad to a comprehensive, but Google often visit is not normal, no way ah.
"C # How to get shortcuts to the target file", the problem of Baidu search for most of the results is to open the shortcut file, the shortcut file in bytes read into the index group, and then write their own code to read the path of the target file by byte, this method beginners are a bit unclear, In particular, it is difficult to use this method to understand the way documents are organized.
in fact, C # is very simple to solve, the principle is to take advantage of iwshruntimelibrary in the iwshshortcut class, you can get the shortcut to the target file.
The following are the specific steps:
One, add a reference to "Windows Script Host Object Model" in the project
Right-click on the project, select Add Reference, select the COM Component tab in the Add Reference dialog box,and click to selectWindows Script Host Object Model, and finalize. "Iwshruntimelibrary" will appear in the projectand you can write the code below.
Second, the Code
It can be implemented with the following code.
1. Refer to these first:
using System.IO; using Iwshruntimelibrary;
2, write the function:
Private stringGets the path that the shortcut file points to (stringpath to the shortcut file) { //path to shortcut file = @ "D:\Test.lnk"; if(System.IO.File.Exists (path to shortcut file)) {WshShell Shell=NewWshShell (); Iwshshortcut Current shortcut file Iwshshortcut class=(iwshshortcut) shell. CreateShortcut (path to shortcut file); //the path to which the shortcut file points. Text = Current Shortcut file Iwshshortcut class. TargetPath; //the destination directory to which the shortcut file points. Text = Current Shortcut file Iwshshortcut class. workingdirectory; returnThe current shortcut file Iwshshortcut class. TargetPath; } Else { return ""; } }
I refer to the original English link:
http://forums.overclockers.co.uk/showthread.php?t=17966879
How C # Gets the target file that the shortcut points to