How to put files in the recycle bin in C #.

zhaozj2021-02-16  62

When my system can provide file operation, I always deal with each delete request as much as possible, because, maybe the user may regret it in this mistake because of this mistake.

/// shfileopstruct definition

[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SHFILEOPSTRUCT {public IntPtr hwnd;. // Window handle to the dialog box to display information about the // status of the file operation public UInt32 wFunc; // Value that indicates which operation to perform public IntPtr pFrom;.. // Address of a buffer to specify one or more source file names // These names must be fully qualified paths Standard Microsoft // MS-DOS wild cards, such as.?? "*", area permitted in the file-name // position. Althought this member is declared as a null-terminated // String, IT IS Used as a buffer to hold multiple file names. Each // file name must be terminated by A Single Null Character. AN // Additional NULL Character Must Be appended to the end of the // final name to indeicate the end of pfrom. public INTPTR PTO; // Address of a buffer to Contain T E Name of the Destination File OR // Directory. This Parameter Must Be Set To Null IT Is Not Used. // Like Pfrom, The PTO Member IS Also A Double-Null Terminated // String and Is Handled In Much The Same Way . public UInt16 fFlags;. // Flags that control the file operation public Int32 fAnyOperationsAborted; // Value that receives TRUE if the user aborted any file operations // before they were completed, or FALSE otherwise public IntPtr hNameMappings.;

// A handle to a name mapping object containing the old and new // names of the renamed files This member is used only if the // fFlags member includes the FOF_WANTMAPPINGHANDLE flag [MarshalAs (UnmanagedType.LPWStr)] public String lpszProgressTitle..; // Address of a string to use as the title of a progress dialog box. // This member is used only if fFlags includes the // FOF_SIMPLEPROGRESS flag.} /// API declaration, using it you can copies, moves, renames, OR DELETES A File System Object. ["Shell32.dll", Charset = Charset.Unicode)] Public Static Extern INT32 SHFILEOPERATION

/// Implementation

Public bool deletefiles (String [] files)

{

SHFILEOPSTRUCT FileOpstruct (); fileopstruct.hwnd = OwnerWindow; fileopstruct.wfunc = (uint) Operation;

String multiSource = StringArrayToMultiString (SourceFiles); String multiDest = StringArrayToMultiString (DestFiles); FileOpStruct.pFrom = Marshal.StringToHGlobalUni (multiSource); FileOpStruct.pTo = Marshal.StringToHGlobalUni (multiDest); FileOpStruct.fFlags = (ushort) OperationFlags; FileOpStruct.lpszProgressTitle = ProgressTitle; Fileopstruct.fanyOperationsAborted = 0; fileopstruct.hnamemappings = intptr.zero;

int RetVal; RetVal = shellapi.shfileOperation (ref fileopstruct);

}

Private string stringArrayTomultIntring (String "StringArray) {string multistring =" ";

IF (StringArray == Null) Return ""

转载请注明原文地址:https://www.9cbs.com/read-17956.html

New Post(0)