Possible Duplicate:
Get target of shortcut folder
For example, in C:\TEMP\
I have a shortcut called test.dll
the shortcut will lead to the file name test.dll
I want to get from the shortcut only the path name to the file it self. So, I'm calling this function in another recursive function and put in this function each time another directory from my hard disk.
For example, the first directory is C:\TEMP
then in C:\TEMP
there is the shortcut file which I want to get the path only to the file. In C:\TEMP
for the test I have now 3 files :
hpwins23.dat
hpwmdl23.dat
hpwmdl23.dat -Shortcut
(C:\TEMP\hpwmdl23.dat
)
So, what I want to get is the path name of the shortcut in this case its C:\TEMP
I tried to use this function:
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;
}
but when I'm using the function and its getting to a shortcut I'm getting exception error on the line:
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink //The exception is: NotImplementedException: The method or operation is not implemented
What shoud I do to solve it ?
This is the full exception error message:
System.NotImplementedException
was caught
Message=The method or operation is not implemented.
Source=GatherLinks
StackTrace:
atShell32.FolderItem.get_GetLink()
atGatherLinks.Form1.GetShortcutTargetFile(String shortcutFilename)
inD:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs
:line
904
atGatherLinks.Form1.offlinecrawling
To view the location of the original file that a shortcut points to, right-click the shortcut and select "Open file location." Windows will open the folder and highlight the original file. You can see the folder path where the file is located in the top of the Windows Explorer window.
Copy method takes three parameters. First the original file with full path, second the file to be copied file name with the new path and third parameter that is optional that is used to overwrite an existing file. If third parameter is true, the Copy method will overwrite if file already exists.
Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function. Syntax: public static void Copy (string sourceFileName, string destFileName);
To get the target of a shortcut (.lnk
file extension) you'll need first to have the following COM
object: Windows Script Host Object Model
Then, you may use WshShell
(or WshShellClass
) and IWshShortcut
interfaces to get the target of a shortcut
Example
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
}
Thanks,
I hope you find this helpful :)
You may try the following steps to add Windows Script Host Object Model
to your project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With