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

40

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

编辑:

我的应用程序无法识别shell32。我应该包含哪些引用或库?我想做的是:

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

我收到的错误信息:

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


Shell32.Shell shell = new Shell32.Shell(); 错误1:找不到类型或命名空间名称“Shell32”(是否缺少 using 指令或程序集引用?) - Lisa
看起来你正在尝试使用外部库。Shell32不是.NET框架的标准组件。你是从其他地方复制的示例吗? - Andrei Marukovich
@David,我相信OP所需要的是“需要哪些using语句...”。@Shaza,如果这是正确的,你需要查看P/Invoke。Shell32作为一个非托管的.dll文件,可以使用反射来调用。更多信息可以在这里找到:https://dev59.com/LkvSa4cB1Zd3GeqPfIzU - Cos Callis
9个回答

63

只需从Windows\System32文件夹中添加对Shell32.dll的引用,然后使用它:

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

我使用64位操作系统,并将应用程序构建为x86,那么应该从同一Windows/system32或"C:\Windows\SysWOW64"中获取引用。这两个文件夹中的Shell32.dll大小不同。 - Romil Kumar Jain
14
您可以在Visual Studio 2010及以上版本的“参考管理器”(“添加引用”对话框窗口)中的COM选项卡中,添加对Microsoft Shell控件和自动化的引用...不确定早期版本是否有此选项。 - Sheridan
@Sheridan:我的“Microsoft Shell Controls And Automation”报告显示它是来自Windows 7 SP1的版本1.0。因此,我认为至少可以在Win7 SP1上使用。 - Joe B
@JoeB,Windows 10 Creator Update已经发布了,但仍然是1.0版本。 - T-moty
添加对“Microsoft Shell 控件和自动化”的引用相当于从“Windows\System32”添加“shell32.dll”。生成的引用相同,仅包含 DLL 的 GUID。 - Andrius R.

61

或许这个可以帮助:

  1. 右键单击项目
  2. 点击添加引用
  3. 添加引用对话框中,点击.COM选项卡
  4. 选择Microsoft Shell Controls and Automation
  5. 点击确定

你的shell32已准备就绪...


32

这个解决方案在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
我在尝试Windows 10 Tech Preview时遇到了COM错误,但这段代码解决了问题。谢谢。 - Mog0
4
你的回答与 Visual Studio 论坛中的“在 Windows 8 中实例化 Shell32.Shell 对象”的问题非常相似。虽然欢迎你更新此问题,但未提供链接就展示他人的工作被称为剽窃,在 Stack Overflow 上是不受欢迎的。如果情况确实如此,请编辑你的回答并提供某种形式的归属声明。 - Sheridan
12
我绝对不是有意抄袭答案,我只是不记得从哪里得到的,而且我也不认为是从那里得到的。在谷歌上快速搜索,这个解决方案无处不在,其中一些甚至比你提供的链接还要早。对此我无话可说。 - Du D.
2
好的,但是以后请在展示他人作品时提供归属。 - Sheridan
这应该是被接受的答案。它在Windows 10上运行良好。 - golden96371

8
  1. 在解决方案资源管理器中右键单击您的项目。
  2. 从下拉菜单中选择“添加引用...”。
  3. 单击“浏览”选项卡。
  4. 导航到 C:\Windows\System32 目录。
  5. 选择 shell32.dll 文件,然后按下“确定”按钮。

现在您已经拥有适用于使用 Shell32.Shell 的适当参考。


5

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


3

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;
            }


        }
    }

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

我喜欢这个。我在 MSDN 上找到了一个教程,并且有一个 MS 工具可以生成它们。该页面名为:“COM Interop Part 1: C# Client Tutorial”,以防我的链接失效。这是链接:https://msdn.microsoft.com/en-us/ie/aa645736%28v=vs.94%29?f=255&MSPPError=-2147217396 - Heriberto Lugo

1

C# .Net 我不完全理解问题,但这对我有效。

[DllImport("Shell32.dll")]
        public static extern int ShellExecuteA(int hwnd, string lpOperation, string lpFile, int lpParameters, int lpDirecotry, int nShowCmd);

private void pictureBox1_Click(object sender, EventArgs e)
        {
            int open;
            open = ShellExecuteA(0, "open", "https://google.com", 0, 0, 1);
        }

使用PictureBox打开链接。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接