0

I want to find every application listed in the user program menu. I use the following routine:

private static void ProcessDirectoryLnkFiles(string path, bool recurse,
    UpdateProcessFromLnkDelegate sProcFile)
{
    try 
    {
        string[] sPrograms = Directory.GetFiles(path, "*.lnk",
            SearchOption.TopDirectoryOnly);

        string[] sSubdirs = Directory.GetDirectories(path);
        Shell32.Shell shell = new Shell32.Shell();

        foreach (string p in sPrograms) {
            Shell32.Folder sLinkFolder;
            Shell32.FolderItem sLinkFolderItem;
            Shell32.ShellLinkObject sLinkObject;
            string sLinkFullpath;

            // Get link full path
            sLinkFullpath = Path.GetFullPath(p);
            // Get link folder
            sLinkFolder = shell.NameSpace(
                Path.GetDirectoryName(sLinkFullpath));
            // Get link item
            sLinkFolderItem = sLinkFolder.Items().
                Item(Path.GetFileName(sLinkFullpath));
            // Get link object
            sLinkObject = (Shell32.ShellLinkObject)
                sLinkFolderItem.GetLink;

            if (sLinkObject.Target.IsFolder == false)
                sProcFile(sLinkObject);
        }

        if (recurse == true)
            foreach (string dir in sSubdirs) 
                ProcessDirectoryLnkFiles(dir, true, sProcFile);
    } 
    catch (UnauthorizedAccessException eUnauthorizedAccessException) {
        sLog.Warn("Unable to iterate on directory {0} ({1}).", 
            path, eUnauthorizedAccessException.Message); 
    } 
    catch (IOException eIOException) {
        sLog.Warn("Unable to iterate on directory {0} ({1}).", 
            path, eIOException.Message);
    } 
    catch (COMException eCOMException) {                
    } 
    catch {
        throw;
    }
 }

This runs quite well on Windows 7 x64. But unfortunately, on Windows XP x86 the Shell32.Shell object doesn't declare the Shell32.Shell.Target property. How do I make this code run on Windows XP?

spoulson
  • 21,335
  • 15
  • 77
  • 102
Luca
  • 11,646
  • 11
  • 70
  • 125

1 Answers1

1

Use the Path property, that gives you the path to the target. System.IO.Directory.Exists() can then tell you if it is directory or not.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • It would be nice if there were an ability to get this kind of information avoiding the use of Shell32 alltogether. – ouflak Oct 09 '13 at 16:12