我有一個顯示檔案夾中檔案的 WPF 應用程式。用戶可以選擇多個檔案并選擇洗掉它們,目前我正在將此邏輯與 VisualBasic.FileIO 庫中的檔案系統一起使用:
foreach (Item item in items)
{
if (item.IsDirectory)
{
FileSystem.DeleteDirectory(item.FullPath, UIOption.AllDialogs, RecycleOption.SendToRecycleBin);
}
else
{
FileSystem.DeleteFile(item.FullPath, UIOption.AllDialogs, RecycleOption.SendToRecycleBin);
}
}
這里的問題是,如果用戶打開了 Windows 選項“顯示洗掉確認對話框”:

他們得到每個檔案的 Windows 提示。
我希望他們得到一個像這樣的提示:

有沒有辦法做到這一點?
即使它涉及某些 WinAPI 函式的 PInvoke?
uj5u.com熱心網友回復:
使用 PInvoke,我們可以使用SHFileOperation和FO_DELETE函式將檔案系統物件發送到回收站。根據檔案,我們可以通過使用 NULL 字符連接多個路徑來一次發送多個路徑:
盡管此成員被宣告為單個以空字符結尾的字串,但它實際上是一個可以保存多個以空字符分隔的檔案名的緩沖區。每個檔案名都以單個 NULL 字符結尾。最后一個檔案名以雙 NULL 字符 ("\0\0") 結束,表示緩沖區結束。
讓我們不要從頭開始撰寫所有內容,而是使用此答案中的部分代碼并對其進行調整以使用多個路徑。我們會有這樣的東西:
public class FileOperationAPIWrapper
{
/// <summary>
/// Possible flags for the SHFileOperation method.
/// </summary>
[Flags]
public enum FileOperationFlags : ushort
{
/// <summary>
/// Do not show a dialog during the process
/// </summary>
FOF_SILENT = 0x0004,
/// <summary>
/// Do not ask the user to confirm selection
/// </summary>
FOF_NOCONFIRMATION = 0x0010,
/// <summary>
/// Delete the file to the recycle bin. (Required flag to send a file to the bin
/// </summary>
FOF_ALLOWUNDO = 0x0040,
/// <summary>
/// Do not show the names of the files or folders that are being recycled.
/// </summary>
FOF_SIMPLEPROGRESS = 0x0100,
/// <summary>
/// Surpress errors, if any occur during the process.
/// </summary>
FOF_NOERRORUI = 0x0400,
/// <summary>
/// Warn if files are too big to fit in the recycle bin and will need
/// to be deleted completely.
/// </summary>
FOF_WANTNUKEWARNING = 0x4000,
}
/// <summary>
/// File Operation Function Type for SHFileOperation
/// </summary>
public enum FileOperationType : uint
{
/// <summary>
/// Move the objects
/// </summary>
FO_MOVE = 0x0001,
/// <summary>
/// Copy the objects
/// </summary>
FO_COPY = 0x0002,
/// <summary>
/// Delete (or recycle) the objects
/// </summary>
FO_DELETE = 0x0003,
/// <summary>
/// Rename the object(s)
/// </summary>
FO_RENAME = 0x0004,
}
/// <summary>
/// SHFILEOPSTRUCT for SHFileOperation from COM
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public FileOperationType wFunc;
public string pFrom;
public string pTo;
public FileOperationFlags fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
public static bool SendToRecycleBin(string path, FileOperationFlags flags)
{
return SendToRecycleBin(new[] { path }, flags);
}
public static bool SendToRecycleBin(IList<string> paths, FileOperationFlags flags)
{
try
{
var fs = new SHFILEOPSTRUCT
{
wFunc = FileOperationType.FO_DELETE,
pFrom = string.Join("\0", paths) '\0' '\0',
fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
};
SHFileOperation(ref fs);
return true;
}
catch (Exception)
{
return false;
}
}
}
用法:
FileOperationAPIWrapper.SendToRecycleBin(items,
FileOperationAPIWrapper.FileOperationFlags.FOF_WANTNUKEWARNING);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/458849.html
上一篇:C#mvvm如何處理視窗關閉事件
