Eliminazione del file per riciclare il cestino su Windows X64 in C#
-
23-09-2019 - |
Domanda
Ho questa classe che sembra funzionare abbastanza bene su non 64 bit.
using System;
using System.Runtime.InteropServices;
namespace DeleteToRecycleBin
{
/// <summary>
/// Send files directly to the recycle bin.
/// </summary>
public class RecybleBin
{
/// <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, Pack = 1)]
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 readonly bool fAnyOperationsAborted;
public readonly IntPtr hNameMappings;
public readonly string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
/// <summary>
/// Send file to recycle bin
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
/// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
public static bool Send(string path, FileOperationFlags flags)
{
try
{
SHFILEOPSTRUCT fs = new SHFILEOPSTRUCT
{
wFunc = FileOperationType.FO_DELETE,
pFrom = path + '\0' + '\0',
fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
};
// important to double-terminate the string.
SHFileOperation(ref fs);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Send file to recycle bin. Display dialog, display warning if files are too big to fit (FOF_WANTNUKEWARNING)
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
public static bool Send(string path) {
return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING);
}
/// <summary>
/// Send file silently to recycle bin. Surpress dialog, surpress errors, delete if too large.
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
public static bool SendSilent(string path)
{
return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT);
}
}
}
Qualche modo per risolverlo in modo che funzioni bene anche su X64? Ho provato a Chaning Ushort a Ulong e un paio di altre modifiche, ma non funziona.
So che ci sono altre soluzioni che richiedono riferimento a Microsoft.VisualBasic ma preferirei P/invoche.
La risposta corretta è:
Ai sensi della X64, lo shfileopstruct deve essere dichiarato senza il parametro pack = 1, oppure fallirà. Questo è un vero dolore se vuoi che il tuo codice sia indipendente dalla piattaforma, poiché devi dichiarare due strutture separate, una con pack = 1 e una senza. Devi quindi dichiarare due diverse chiamate shfileoperation, una per ciascuna delle strutture. Quindi devi decidere quale chiamare a seconda che tu stia correndo su 32 o 64 bit.
Soluzione
Ecco cosa faccio:
yepnope.addPrefix('less', function(resourceObj) {
resourceObj.forceCSS = true;
resourceObj.attrs = {
'rel' : "stylesheet/less",
'type': "text/css"
};
return resourceObj;
}
.
Ciò creerà una funzione prefisso yepnope per qualsiasi URL che inizia con less!
.La funzione lo costringerà a essere caricato come CSS e posizionerà anche gli attributi rel
e type
nell'elemento <link>
, che sono richiesti per meno analizzare i file.Assicurati di utilizzare Yepnope 1.5+ o ModernizR.
Quindi, nelle tue istruzioni di carico:
yepnope({
load: [
'less!path/to/file.less',
'less!path/to/file2.less',
...
'path/to/less.js'
]
});
.
Quale caricherà per la prima volta i file *.less
e quindi elaborarli con meno JavaScript.
Altri suggerimenti
Per quanto strano sembra, .NET ha già funzioni da eliminare nel cestino ... ma sono in Microsoft.VisualBasic
spazio dei nomi. In particolare, Microsoft.VisualBasic.FileIO
.
using Microsoft.VisualBasic.FileIO;
// class declaration, method start here
// Send file to recycle bin without dialog
FileSystem.DeleteFile("pathToFile", UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
// Send file to recycle bin without dialog
FileSystem.DeleteFile("pathToFile", UIOption.AllDialogs, RecycleOption.SendToRecycleBin);
// Directories are the same, but with DeleteDirectory instead