Shell32.dll .NET 接口依赖于操作系统

发布于 2024-10-11 15:45:45 字数 1955 浏览 2 评论 0原文

我想找到用户程序菜单中列出的每个应用程序。我使用以下例程:

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 上运行?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-10-18 15:45:45

使用 Path 属性,它为您提供目标的路径。然后 System.IO.Directory.Exists() 可以告诉您它是否是目录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文