35

为了使 Shell32 工作,我应该在 C# 应用程序中包含什么?

编辑:

我的应用程序无法识别 shell32。我应该包括哪些参考资料或库?我想做的是:

Shell32.Shell shell = new Shell32.Shell(); 

我得到的错误是:

错误 1 ​​找不到类型或命名空间名称“Shell32”(您是否缺少 using 指令或程序集引用?)

4

9 回答 9

59

只需Shell32.dllWindows\System32文件夹中添加一个引用并使用它:

Shell32.Shell shell = new Shell32.Shell();
shell.MinimizeAll();
于 2011-04-18T20:35:48.300 回答
56

也许这可以帮助:

  1. 右键项目
  2. 点击Add reference
  3. 单击.COM选项卡Add reference对话框
  4. 选择Microsoft Shell Controls and Automation
  5. 点击OK

shell32已经准备好使用...

于 2013-09-19T12:32:24.530 回答
29

我知道这个帖子很旧,但我把这个贴给和我有同样问题的人。上面的解决方案在windows 8下无法编译

Shell32.Shell 外壳 = 新 Shell32.Shell(); <= 这不适用于 Windows 8

如果您希望您的应用程序在 Windows 8 下运行,请使用以下解决方法。

using Shell32;

private Shell32.Folder GetShell32Folder(string folderPath)
{
    Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
    Object shell = Activator.CreateInstance(shellAppType);
    return (Shell32.Folder)shellAppType.InvokeMember("NameSpace",
    System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folderPath });
}
于 2013-09-26T17:44:55.450 回答
7
  1. 在解决方案资源管理器中右键单击您的项目。
  2. 从下拉菜单中选择“添加参考...”。
  3. 单击“浏览”选项卡。
  4. 导航到 C:\Windows\System32 目录。
  5. 选择“shell32.dll”文件。并按下“确定”按钮。

您现在有了使用 Shell32.Shell 的适当参考。

于 2013-02-22T19:29:35.697 回答
4

向您的项目添加对 COM 库Microsoft Shell Controls and Automation的引用。此外,确保使用的代码Shell32.Shell在单线程单元中运行,例如通过将[STAThread]属性添加到Main.

于 2019-01-17T19:48:58.910 回答
3

我猜您在识别任何呼叫时遇到问题,所以我建议您参考这篇一般文章: http: //www.codeproject.com/KB/shell/csdoesshell1.aspx

除此之外,您还需要提供对您不起作用的细节。

于 2011-04-18T20:30:01.937 回答
2

下面显示的类应该对 C# 中 shell32 的一些方法有所帮助。您应该通过右键单击项目在参考窗口中添加“Microsoft Shell 命令和自动化”的参考。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    namespace MusicMuttPrototype
    {
        public class clsShellFileInfo
        {
            public Exception errorException;
            public enum FileDetailInfo
            {
                Name = 0,
                Year = 15,
                Size = 1,
                Track_Number = 19,
                Type = 2,
                Genre = 20,
                Date_Modified = 3,
                Duration = 27,
                Date_Created = 4,
                Bit_Rate = 28,
                Date_Accessed = 5,
                Protected = 23,
                Attributes = 6,
                Camera_Model = 24,
                Status = 7,
                Date_Picture_Taken = 25,
                Owner = 8,
                Dimensions = 26,
                Author = 9,
                Not_used = 27,
                Title = 10,
                Not_used_file = 28,
                Subject = 11,
                //Not_used = 29,
                Category = 12,
                Company = 30,
                Pages = 13,
                Description = 31,
                Comments = 14,
                File_Version = 32,
                Copyright = 15,
                Product_Name_Chapter = 33,
                //Scripting Quicktest Profess11ional Page 63 
                Artist = 16,
                Product_Version = 34,
                Album_Title = 17,
                Retrieves_the_info_tip_inf = -1
            }

            public string getFileDetails(string fileFolder, string filePath, FileDetailInfo infotype)
            {
                string strReturnval = "";
                try
                {
                    Shell32.Shell fileshell = new Shell32.Shell();
                    Shell32.Folder fileshellfolder = fileshell.NameSpace(fileFolder);
                    Shell32.FolderItem Item = fileshellfolder.ParseName(filePath);
                    strReturnval = fileshellfolder.GetDetailsOf(Item, (int)infotype);
                }
                catch (Exception ex)
                {

                    errorException = ex;
                }
                return strReturnval;
            }


        }
    }
于 2013-12-22T09:50:40.210 回答
2

如果您不需要完整的 API 调用集,最好创建一个 COM 导入存根类。看看编写 Desk Drive 的 Mike Ward 是如何做到的。

http://mike-ward.net/2008/09/02/a-lean-method-for-invoking-com-in-c/ https://github.com/mike-ward/DeskDrive/blob/master/ src/DeskDrive/Shell32.cs

于 2016-10-19T21:57:11.230 回答
1

不推荐引用实际的 shell32.dll。您将在 .NET Framework 4+ 中遇到错误。使用较旧的 .NET Framework 只是为了使用 shell32.dll 会限制程序的功能。在 Windows 7+ 和 .NET Framework 4+ 的应用程序中,您应该始终使用 .COM 组件。右键项目。单击添加参考。单击添加引用对话框中的 .COM 选项卡。选择 Microsoft 外壳控件和自动化。点击确定

于 2018-07-09T16:18:26.873 回答