如何在C#应用程序中使用Shell32?

为了使Shell32工作,我应该在C#应用程序中包含哪些内容?

编辑:

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

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

我得到的是一个错误:

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

只需在Windows/system32文件夹中Add ReferenceShell32.dll Add Reference并使用它:

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

也许这可以帮助:

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

你的shell32可以使用……

我知道这个post已经过时了,但是我发布这个post给任何有同样问题的人都这样做了。 上面的解决方案不能在Windows 8下编译

Shell32.Shell shell = new 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 }); } 
  1. 在解决方案资源管理器中右键单击您的项目。
  2. 从下拉菜单中选择“添加参考…”。
  3. 单击“浏览”选项卡。
  4. 导航到C:\ Windows \ System32目录。
  5. 选择“shell32.dll”文件。 然后按“确定”按钮。

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

我猜你在识别任何电话时都遇到了麻烦,所以我建议你参考这篇一般文章: http : //www.codeproject.com/KB/shell/csdoesshell1.aspx

除此之外,您还需要提供不适合您的具体信息。

下面显示的类应该有助于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; } } } 

如果您不需要完整的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

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