3

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?

Nathan
  • 59
  • 6
  • 1
    Remove try/catch from your code and try again so you can properly document your problem. – Hans Passant Mar 12 '16 at 12:40
  • I'll try, but my problem is that I can't run the debugger on the Citrix environment so I'm making do with messageboxes atm. In any case I'm fairly sure that the code breaks at that line since any messagebox before it fires but any after doesn't. The question was more if there was another way to get the shortcut target though? – Nathan Mar 12 '16 at 12:52
  • Without the try/catch the application simply crashes when it gets to a shortcut it needs to check using that code. Since I don't have access to the debugger in Citrix that's all I get unfortunately :/ – Nathan Mar 12 '16 at 13:04
  • Every .NET programmer eventually discovers that writing an event handler for AppDomain.CurrentDomain.UnhandledException is *not* optional. You are not done yet. – Hans Passant Mar 12 '16 at 13:05
  • You might give [this answer](http://stackoverflow.com/a/19035049/578411) a try. Looks like that COM object doesn't want to get instantiated. – rene Mar 12 '16 at 15:00
  • Super - thank you @rene! I've been locked out of VPN temporarily but I'll give it a go and report back as soon as I'm back at work on monday :) – Nathan Mar 12 '16 at 16:31

1 Answers1

0

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."

Community
  • 1
  • 1
Nathan
  • 59
  • 6