In a C# program I want to be able to identify the file locations of LNK files (the actual file location, not the LNK location). But sometimes ShellLink is returning an old location for a file. For instance it is returning "C:\Program Files (x86)\XXX\xxx.exe" instead of "C:\Program Files\XXX\xxx.exe" (the actual text in the LNK file). I thought that the old location might be cached in the registry but could not find that string. Any ideas on how to ensure that ShellLink identifies the correct location or to fix this particular "bad" link? I have re-created the link and rebooted but that does not help.
/// <summary>Given a LNK file, return the file that it points to.</summary>
public static string ResolveShortcut(string filename)
{
ShellLink link = new ShellLink();
((IPersistFile)link).Load(filename, STGM_READ);
StringBuilder sb = new StringBuilder(MAX_PATH);
WIN32_FIND_DATAW data = new WIN32_FIND_DATAW();
((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
if (sb.Length == 0 && !filename.EndsWith(".LNK", StringComparison.OrdinalIgnoreCase)) sb.Append(filename);
return sb.ToString();
}