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

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

///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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值