简体   繁体   English

SHQueryRecycleBin 不适用于 .net Core

[英]SHQueryRecycleBin Not Working on .net Core

I have Methods Out of which : 1 Use "SHQueryRecycleBin" to Query Recycle Bin & Other Use "SHEmptyRecycleBin" To Delete Recycle Bin, Both of em Works Fine without any issue on my .Net 4.8 Projects.我有以下方法: 1 使用“SHQueryRecycleBin”查询回收站和其他使用“SHEmptyRecycleBin”删除回收站,两者都可以正常工作,在我的.Net 4.8项目上没有任何问题。

But While i am trying to use the same code on .Net Core 3.1 "SHQueryRecycleBin" Simply Refuse To Work & Throw "HRESULT -2147024809" .但是虽然我试图在.Net Core 3.1 "SHQueryRecycleBin"上使用相同的代码,只是拒绝工作并抛出"HRESULT -2147024809"

To Check I Tried it on {.Net 4.8} vs {.Net Core 3.1} Console, WinForm & Wpf App & it worked Everytime on .net Framework.检查我在 {.Net 4.8} 与 {.Net Core 3.1} 控制台、WinForm 和 Wpf 应用程序上尝试过,它每次都在 .net 框架上工作。

Code Sample :代码示例:

[DllImport("shell32.dll")]
static extern int SHQueryRecycleBin(string pszRootPath, ref 
SHQUERYRBINFO  pSHQueryRBInfo);

[StructLayout(LayoutKind.Sequential, Pack=1)] // also tried pack=4 
public struct SHQUERYRBINFO
{
    public int  cbSize;
    public ulong i64Size;
    public ulong i64NumItems;
}

public static int GetCount()
{
    SHQUERYRBINFO sqrbi = new SHQUERYRBINFO();
    sqrbi.cbSize = Marshal.SizeOf(typeof(SHQUERYRBINFO));
    int hresult = SHQueryRecycleBin(string.Empty, ref sqrbi);
    return (int)sqrbi.i64NumItems;
}

I had an inkling as to what the problem might be before I tried your code.在尝试您的代码之前,我对问题可能是什么有所了解。

If you run your code as is, you receive a HRESULT of -2147024809 from SHQueryRecycleBin .如果按原样运行代码,则会从SHQueryRecycleBin收到-2147024809HRESULT

This is actually a HRESULT of 0x80070057 , which is the error ERROR_INVALID_PARAMETER (or ' The parameter is incorrect ' if you prefer plain English).这实际上是0x80070057HRESULT ,这是错误ERROR_INVALID_PARAMETER (或“参数不正确”,如果您更喜欢简单的英语)。 This indicates that there is an issue with one or more of the parameters being sent to the Win32 call.这表明发送到 Win32 调用的一个或多个参数存在问题。

In the case of your code, the culprit is your StructLayout declaration of SHQUERYRBINFO .就您的代码而言,罪魁祸首是您的SHQUERYRBINFOStructLayout声明。 You have defined a Pack alignment size of 1 (and tried 4), but are using ulong s in the struct .您已将Pack对齐大小定义为 1(并尝试了 4),但在struct中使用了ulong A Win32 ulong is 8 bytes, so you should use 8 as the Pack size in .Net Core: Win32 ulong是 8 字节,因此您应该使用8作为 .Net Core 中的Pack大小:

...

[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct SHQUERYRBINFO
{
    ...

Incidentally, if you omit the Pack parameter completely, the default value of 8 will be used.顺便说一句,如果完全省略Pack参数,则将使用默认值 8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM