CSDN博客

img beyond_ml

在C#中如何将文件放入回收站。

发表于2004/7/2 22:09:00  1010人阅读

分类: .NetFramework Csharp

当我的系统可以提供文件操作的时候,我总是尽可能小心对付每一次删除请求,因为,也许用户有可能会在下一分钟因为这次的误操作而后悔不已。

///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 "*", are permitted in the file-name
             // position. Although 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 indicate the end of pFrom.
   public IntPtr pTo;      // Address of a buffer to contain the name of the destination file or
             // directory. This parameter must be set to NULL if 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.
  [DllImport("shell32.dll" , CharSet = CharSet.Unicode)]
  public static extern Int32 SHFileOperation(
   ref SHFILEOPSTRUCT lpFileOp);

///implementation

public bool DeleteFiles(string[] files)

{

   SHFILEOPSTRUCT FileOpStruct = new SHFILEOPSTRUCT();
   
   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 StringArrayToMultiString(String[] stringArray)
  {
   String multiString = "";

   if (stringArray == null)
    return "";

   for (int i=0 ; i<stringArray.Length ; i++)
    multiString += stringArray[i] + '/0';
   
   multiString += '/0';
   
   return multiString;
  }

0 0

相关博文

我的热门文章

img
取 消
img