我想在“用户程序”菜单中找到所有列出的应用程序。我使用以下例程:

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;
    }
 }

这在Windows 7 x64上运行良好。但不幸的是,在Windows XP X86上 Shell32.Shell 对象不会声明 Shell32.Shell.Target 财产。如何使此代码在Windows XP上运行?

有帮助吗?

解决方案

使用路径属性,为您提供目标路径。 System.io.directory.exists()然后可以告诉您是否是目录。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top