Reputation: 59
I'm not a very experienced programmer and new at C#, and I'm having some problems getting the target of a shortcut using shell32.shell(). I found the code here on stackoverflow - and it works beautifully on regular Windows PC's, but when executed on a Citrix virtualized Windows desktop (where I need it to run) it breaks.
The code runs through shortcuts in a folder via a foreach loop, and filters out any that has an executable target. Problem is that to find the target of the shortcut I use the code below, and as soon as it is called the foreach breaks and doesn't progress any further (on Citrix).
I have determined that the break happens at the line "var shl = new Shell32.Shell();", the code after that line doesn't fire and it exits the foreach (but continues executing code after the foreach).
public static string GetLnkTarget(string lnkPath)
{
var shl = new Shell32.Shell();
lnkPath = System.IO.Path.GetFullPath(lnkPath);
var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath));
var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath));
var lnk = (Shell32.ShellLinkObject)itm.GetLink;
return lnk.Target.Path;
}
Does anyone know of an alternate way to find the target of a shortcut that'll work in a Citrix virtualized environment?
Upvotes: 3
Views: 2779
Reputation: 59
I don't know what caused the issue with Shell32.Shell() specifically in the Citrix environment, but I found another way that works for me. The answer was provided by user djdanlib here: https://stackoverflow.com/a/8661371/5992820
"Add IWshRuntimeLibrary as a reference to your project. Add Reference, COM tab, Windows Scripting Host Object Model. Here is how I get the properties of a shortcut:
IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(filename);
The shortcut object "sc" has a TargetPath property."
Upvotes: 0