How C # Gets the target file that the shortcut points to

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.