为了使 Shell32 工作,我应该在 C# 应用程序中包含什么?
编辑:
我的应用程序无法识别 shell32。我应该包括哪些参考资料或库?我想做的是:
Shell32.Shell shell = new Shell32.Shell();
我得到的错误是:
错误 1 找不到类型或命名空间名称“Shell32”(您是否缺少 using 指令或程序集引用?)
只需Shell32.dll
从Windows\System32
文件夹中添加一个引用并使用它:
Shell32.Shell shell = new Shell32.Shell();
shell.MinimizeAll();
也许这可以帮助:
Add reference
.COM
选项卡Add reference
对话框Microsoft Shell Controls and Automation
你shell32
已经准备好使用...
我知道这个帖子很旧,但我把这个贴给和我有同样问题的人。上面的解决方案在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 });
}
您现在有了使用 Shell32.Shell 的适当参考。
向您的项目添加对 COM 库Microsoft Shell Controls and Automation的引用。此外,确保使用的代码Shell32.Shell
在单线程单元中运行,例如通过将[STAThread]
属性添加到Main
.
我猜您在识别任何呼叫时遇到问题,所以我建议您参考这篇一般文章: 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 会限制程序的功能。在 Windows 7+ 和 .NET Framework 4+ 的应用程序中,您应该始终使用 .COM 组件。右键项目。单击添加参考。单击添加引用对话框中的 .COM 选项卡。选择 Microsoft 外壳控件和自动化。点击确定