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

发布于 2024-11-02 17:12:40 字数 295 浏览 1 评论 0原文

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

编辑:

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

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

我收到的错误是:

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

What should I include within a C# application in order to make Shell32 work?

Edit:

My application can't recognize shell32. What references or lib should I include? What I'm trying to do is:

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

What I'm getting as an error:

Error 1 The type or namespace name 'Shell32' could not be found (are you missing a using directive or an assembly reference?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

燕归巢 2024-11-09 17:12:40

也许这可以帮助:

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

您的shell32 已准备好使用...

maybe this can help:

  1. Right click project
  2. Click Add reference
  3. Click .COM tab in Add reference dialogue
  4. Select Microsoft Shell Controls and Automation
  5. Click OK

your shell32 is ready to use...

眼前雾蒙蒙 2024-11-09 17:12:40

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

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

Just add a reference to Shell32.dll from the Windows\System32 folder and use it:

Shell32.Shell shell = new Shell32.Shell();
shell.MinimizeAll();
新雨望断虹 2024-11-09 17:12:40

此解决方案无法在 Windows 8 及更高版本下编译。为此,可以在此处找到解决方法< /a>:

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

This solution does not compile under windows 8 and later. For that, a workaround can be found here:

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 });
}
鲜血染红嫁衣 2024-11-09 17:12:40
  1. 在解决方案资源管理器中右键单击您的项目。
  2. 从下拉菜单中选择“添加引用...”。
  3. 单击“浏览”选项卡。
  4. 导航到 C:\Windows\System32 目录。
  5. 选择 shell32.dll 文件。并按“确定”按钮。

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

  1. Right click your project in the solution explorer.
  2. Choose "Add Reference..." from the drop-down menu.
  3. Click the "Browse" tab.
  4. Navigate to the C:\Windows\System32 directory.
  5. Choose the shell32.dll file. and press the "OK" button.

You now have the appropriate reference for using Shell32.Shell.

罪歌 2024-11-09 17:12:40

将 COM 库 Microsoft Shell 控制和自动化 的引用添加到您的项目中。此外,请确保使用 Shell32.Shell 的代码在单线程单元中运行,例如,通过将 [STAThread] 属性添加到 Main 中。

Add to your project a reference to the COM library Microsoft Shell Controls and Automation. Additionally, ensure that the code using Shell32.Shell is running in a single threaded apartment, e.g. by adding the [STAThread] attribute to Main.

梦晓ヶ微光ヅ倾城 2024-11-09 17:12:40

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


        }
    }

The class shown below should help with some of the methods of shell32 in C# .
you should add the reference of "Microsoft Shell command and automation" with the reference window by righting clicking the project .

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


        }
    }
不…忘初心 2024-11-09 17:12:40

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

链接 1

链接2

If you don't need the full set of API calls, you maybe better off creating a COM import stub class. See how Mike Ward who wrote Desk Drive did it.

Link 1

Link 2

感性不性感 2024-11-09 17:12:40

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

使用图片框打开链接。

C# .Net
I do not understand the full question but this is working for me.

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

Using a picturebox to open a link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文