Reputation: 1055
How do you get the directory target of a shortcut folder? I've search everywhere and only finds target of shortcut file.
Upvotes: 24
Views: 33215
Reputation: 13452
I think you will need to use COM and add a reference to "Microsoft Shell Control And Automation", as described in this answer.
Here's example code to use it (from the now defunct http://www.saunalahti.fi/janij/blog/2006-12.html#d6d9c7ee-82f9-4781-8594-152efecddae2 ):
namespace Shortcut
{
using System;
using System.Diagnostics;
using System.IO;
using Shell32;
class Program
{
public static 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);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}
return string.Empty;
}
static void Main(string[] args)
{
const string path = @"C:\link to foobar.lnk";
Console.WriteLine(GetShortcutTargetFile(path));
}
}
}
Upvotes: 33
Reputation: 376
I have used this, always based on builtin windows "Shell.Application" COM object, but implemented using pure late-binding and reflection, thus without any need to add a reference to the existing project, and without any dependencies.
public static string GetLnkTarget(string lnkPath)
{
if (!System.IO.File.Exists(lnkPath))
throw new FileNotFoundException();
Type shlType;
object shl = null;
object dir = null;
object items = null;
object itm = null;
object lnk = null;
try
{
//var shl = new Shell();
shlType = Type.GetTypeFromProgID("Shell.Application");
shl = Activator.CreateInstance(shlType);
string dirName = Path.GetDirectoryName(lnkPath);
string lnkFile = Path.GetFileName(lnkPath);
//var dir = shl.NameSpace(dirName);
dir = shlType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shl, new object[] { dirName });
//var itm = dir.Items().Item(lnkFile);
items = dir.GetType().InvokeMember("Items", System.Reflection.BindingFlags.InvokeMethod, null, dir, null);
itm = items.GetType().InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, items, new object[] { lnkFile });
//var lnk = (ShellLinkObject)itm.GetLink;
lnk = itm.GetType().InvokeMember("GetLink", System.Reflection.BindingFlags.GetProperty, null, itm, null);
//return lnk.Path;
object path = lnk.GetType().InvokeMember("Path", System.Reflection.BindingFlags.GetProperty, null, lnk, null);
return (string)path;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (lnk != null) Marshal.ReleaseComObject(lnk);
if (itm != null) Marshal.ReleaseComObject(itm);
if (items != null) Marshal.ReleaseComObject(items);
if (dir != null) Marshal.ReleaseComObject(dir);
if (shl != null) Marshal.ReleaseComObject(shl);
}
}
Upvotes: 0
Reputation: 1
public static string GetLnkTarget(string lnkPath)
{
var shl = new Shell();
var dir = shl.NameSpace(Path.GetDirectoryName(lnkPath));
var itm = dir.Items().Item(Path.GetFileName(lnkPath));
var lnk = (ShellLinkObject)itm.GetLink;
if (!File.Exists(lnk.Path)){
return lnk.Path.Replace("Program Files (x86)", "Program Files");
}
else{
return lnk.Path;
}
}
Upvotes: 0
Reputation: 167
In windows 10 it needs to be done like this, first add COM reference to "Microsoft Shell Control And Automation"
// new way for windows 10
string targetname;
string pathOnly = System.IO.Path.GetDirectoryName(LnkFileName);
string filenameOnly = System.IO.Path.GetFileName(LnkFileName);
Shell shell = new Shell();
Shell32.Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null) {
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
targetname = link.Target.Path; // <-- main difference
if (targetname.StartsWith("{")) { // it is prefixed with {54A35DE2-guid-for-program-files-x86-QZ32BP4}
int endguid = targetname.IndexOf("}");
if (endguid > 0) {
targetname = "C:\\program files (x86)" + targetname.Substring(endguid + 1);
}
}
Upvotes: 7
Reputation: 53
An even simpler way to get the linked path that I use is:
private static string LnkToFile(string fileLink)
{
string link = File.ReadAllText(fileLink);
int i1 = link.IndexOf("DATA\0");
if (i1 < 0)
return null;
i1 += 5;
int i2 = link.IndexOf("\0", i1);
if (i2 < 0)
return link.Substring(i1);
else
return link.Substring(i1, i2 - i1);
}
But it will of course break if the lnk-file format changes.
Upvotes: 0
Reputation: 257
if you want find your application path that has shortcut on desktop, an easy way that i use, is the following:
Process.GetCurrentProcess().MainModule.FileName.Substring(0, Process.GetCurrentProcess().MainModule.FileName.LastIndexOf("\\")
this code return any exe path that is running,Regardless that who requested file
Upvotes: -2
Reputation: 45
All file shortcuts have a .lnk file extension you can check for. Using a string for example, you could use string.EndsWith(".lnk") as a filter.
All URL shortcuts have a .url file extension, so you will need to account for those as well if needed.
Upvotes: -4