简体   繁体   English

如何检测驱动器在C#中是否有回收站?

[英]How do I detect if a drive has a recycle bin in C#?

I have an application which uses the FOF_ALLOWUNDO with SHFileOperation in order to move files to the recycle bin. 我有一个应用程序,它使用FOF_ALLOWUNDO和SHFileOperation将文件移动到回收站。

Some removable drives do not have a recycle bin. 某些可移动驱动器没有回收站。 In this case SHFileOperation deletes the files directly. 在这种情况下,SHFileOperation直接删除文件。 I want to give a warning to the user that the files are going to be deleted directly. 我想向用户发出警告,要求直接删除文件。

In order to do this I need to know if the drive has a recycle bin. 为了做到这一点,我需要知道驱动器是否有回收站。

Use FOF_WANTNUKEWARNING. 使用FOF_WANTNUKEWARNING。

Send a warning if a file is being permanently destroyed during a delete operation rather than recycled. 如果文件在删除操作期间被永久销毁而不是回收,则发送警告。 This flag partially overrides FOF_NOCONFIRMATION. 此标志部分覆盖FOF_NOCONFIRMATION。

I found a function called SHQueryRecycleBin when I looked at the functions exported by shell32.dll. 当我查看shell32.dll导出的函数时,我找到了一个名为SHQueryRecycleBin的函数。

If the drive specified in pszRootPath has a recycle bin the function returns 0 otherwise it returns -2147467259. 如果pszRootPath中指定的驱动器具有回收站,则该函数返回0,否则返回-2147467259。

I'm going to use this function via PInvoke. 我将通过PInvoke使用此功能。

I used the P/Invoke Interop Assistant to create the PInvoke code. 我使用P / Invoke Interop助手来创建PInvoke代码。

Here is the code of my function DriveHasRecycleBin : 这是我的函数DriveHasRecycleBin的代码:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    private struct SHQUERYRBINFO
    {
        /// DWORD->unsigned int
        public uint cbSize;

        /// __int64
        public long i64Size;

        /// __int64
        public long i64NumItems;
    }

    /// Return Type: HRESULT->LONG->int
    ///pszRootPath: LPCTSTR->LPCWSTR->WCHAR*
    ///pSHQueryRBInfo: LPSHQUERYRBINFO->_SHQUERYRBINFO*
    [System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint = "SHQueryRecycleBinW")]
    private static extern int SHQueryRecycleBinW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)] string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);

    public bool DriveHasRecycleBin(string Drive)
    {
        SHQUERYRBINFO Info = new SHQUERYRBINFO();
        Info.cbSize = 20; //sizeof(SHQUERYRBINFO)
        return SHQueryRecycleBinW(Drive, ref Info) == 0;
    }

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

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