📜  shell32.dll c# 示例 - C# (1)

📅  最后修改于: 2023-12-03 15:20:08.030000             🧑  作者: Mango

Shell32.dll in C#

Shell32.dll is a dynamic link library (DLL) file included in Microsoft Windows that contains functions for working with the Windows shell. It exposes a set of APIs for a wide range of shell-related tasks, such as manipulating files and folders, launching applications, and working with various shell objects.

In C#, the Shell32.dll can be accessed using Platform Invocation Services (P/Invoke) or by using the Shell32 COM object. In this article, we will explore both approaches.

Using P/Invoke

To use Shell32.dll functions with P/Invoke, we need to define the method signatures of the functions we want to use. We can find the method signatures in the official documentation. Here's an example of how to retrieve the path of the desktop folder using SHGetFolderPath function:

using System;
using System.Runtime.InteropServices;

class Program
{
    static void Main()
    {
        string desktopPath = GetDesktopFolder();
        Console.WriteLine(desktopPath);
    }

    static string GetDesktopFolder()
    {
        const int CSIDL_DESKTOP = 0x0000;
        const uint SHGFP_TYPE_CURRENT = 0;
        var path = new StringBuilder(260);
        SHGetFolderPath(IntPtr.Zero, CSIDL_DESKTOP, IntPtr.Zero, SHGFP_TYPE_CURRENT, path);
        return path.ToString();
    }

    [DllImport("shell32.dll")]
    private static extern int SHGetFolderPath(
      IntPtr hwndOwner,
      int nFolder,
      IntPtr hToken,
      uint dwFlags,
      StringBuilder pszPath
    );
}

In this code, we define the SHGetFolderPath method signature using the DllImport attribute. We then call this method using the P/Invoke mechanism to retrieve the path of the desktop folder.

Using Shell32 COM Object

Another way to use Shell32.dll functions is by using the Shell32 COM object. We can add a reference to this COM object in our C# project and create an instance of it to access its methods. Here's an example of how to create a new folder on the desktop using the Shell32 COM object:

using Shell32;

class Program
{
    static void Main()
    {
        string newFolderName = "New Folder";
        CreateNewFolder(newFolderName);
    }

    static void CreateNewFolder(string folderName)
    {
        var shell = new Shell();
        Folder desktopFolder = shell.NameSpace("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}").Self;
        Folder newFolder = desktopFolder.Items().Add(folderName, 0).GetFolder;
    }
}

In this code, we create an instance of the Shell class and use the NameSpace method to obtain a reference to the desktop folder. We then use the Items method to add a new folder to the desktop and retrieve a reference to it.

Conclusion

Shell32.dll provides a wide range of functions for working with the Windows shell. In C#, we can access these functions using P/Invoke or by using the Shell32 COM object. With these approaches, we can perform shell-related tasks such as manipulating files and folders, launching applications, and working with various shell objects.