win7环境下使用Shell32遍历系统文件夹,如下例:
Shell32.Shell shell32 = new Shell32.ShellClass();
Shell32.Folder shell32Folder = shell32.NameSpace(Win32API.ShellFolder.MyComputer);
Shell32.Folder desktopFolder = shell32.NameSpace(Win32API.ShellFolder.DesktopDirectory);
Shell32.FolderItems folderItems = desktopFolder.ParentFolder.Items();
foreach(Shell32.FolderItem folderItem in folderItems)
 { 
   if(folderItem.Name == desktopFolder.Title)
   {
      _parentListViewItem = new FolderListViewItem(folderItem);
      break;
   }
 }
但是部署在winxp上报错:
System.InvalidCastException: 无法将类型为“Shell32.ShellClass”的 COM 对象强制转换为接口类型“Shell32.IShellDispatch5”。
此操作失败的原因是对 IID 为“{866738B9-6CF2-4DE8-8767-F794EBE74F4E}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口...====================================================
只有将程序在winxp上重新编译一下,才可以。请问能否不需要在xp重新编译的情况下,正常部署。

解决方案 »

  1.   

    问题好像是获取系统图标代码的地方,因为在wi7下能够正常执行下列代码
      // Get the System IImageList object from the Shell:
                    Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
                    int ret = SHGetImageList(
                        (int)size,
                        ref iidImageList,
                        ref iImageList
                        );
    在win xp下就报错。但是参照网上一段代码:
        /// <summary>
            /// Creates the SystemImageList
            /// </summary>
            private void create()
            {
                // forget last image list if any:
                hIml = IntPtr.Zero;            if (isXpOrAbove())
                {
                    // Get the System IImageList object from the Shell:
                    Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
                    int ret = SHGetImageList(
                        (int)size,
                        ref iidImageList,
                        ref iImageList
                        );
                    // the image list handle is the IUnknown pointer, but 
                    // using Marshal.GetIUnknownForObject doesn't return
                    // the right value.  It really doesn't hurt to make
                    // a second call to get the handle:
                    SHGetImageListHandle((int)size, ref iidImageList, ref hIml);
                }
                else
                {
                    // Prepare flags:
                    SHGetFileInfoConstants dwFlags = SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | SHGetFileInfoConstants.SHGFI_SYSICONINDEX;
                    if (size == SysImageListSize.smallIcons)
                    {
                        dwFlags |= SHGetFileInfoConstants.SHGFI_SMALLICON;
                    }
                    // Get image list
                    SHFILEINFO shfi = new SHFILEINFO();
                    uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType());                // Call SHGetFileInfo to get the image list handle
                    // using an arbitrary file:
                    hIml = SHGetFileInfo(
                        ".txt",
                        FILE_ATTRIBUTE_NORMAL,
                        ref shfi,
                        shfiSize,
                        (uint)dwFlags);
                    System.Diagnostics.Debug.Assert((hIml != IntPtr.Zero), "Failed to create Image List");
                }
            }
    也不行啊。
      

  2.   

    出错的地方是:
    Shell32.Shell shell32 = new Shell32.ShellClass();
    Shell32.Folder shell32Folder = shell32.NameSpace(ShellFolder.MyComputer);就是第二句话
      

  3.   

    Shell32不是系统DLL 吗?如果不是你在XP 上面注册一下regsvr32 Shell32.dll
      

  4.   

    Win7 和 XP 的 Shell32.dll 文件不同吧
      

  5.   

    好像要使用
      Type shellType = Type.GetTypeFromProgID("Shell.Application");
      object shellObject = System.Activator.CreateInstance(shellType);
    具体如何使用,有人用过吗?
      

  6.   

    在Win7下运行,就用 win7的 System32.dll
    在XP 下运行,就用 XP的 System32.dll不需要重新编译。试试看。