C#删除文件和文件夹到回收站

c# 如何将文件删除到回收站(文件操作)

长空玮

很多一部分网上回答都是调用System.VisualBasic的,这部分我就不粘了,毕竟不是使用VB的。以下2种方法基本相同,主要是鉴于构造不同,决定都附上。

1.(http://www.dotblogs.com.tw/larrynung/archive/2012/09/25/75026.aspx?fid=43791)

using System;
using System.Runtime.InteropServices;

namespace ceshiConsole{
public class FileIOHelper

{

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]

public struct SHFILEOPSTRUCT

{

public IntPtr hwnd;

[MarshalAs(UnmanagedType.U4)]

public int wFunc;

public string pFrom;

public string pTo;

public short fFlags;

[MarshalAs(UnmanagedType.Bool)]

public bool fAnyOperationsAborted;

public IntPtr hNameMappings;

public string lpszProgressTitle;

}


#region Dllimport

[DllImport("shell32.dll", CharSet = CharSet.Auto)]

public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

#endregion

#region Const

public const int FO_DELETE = 3;

public const int FOF_ALLOWUNDO = 0x40;

public const int FOF_NOCONFIRMATION = 0x10;

#endregion


#region Public Static Method

public static void DeleteFileToRecyclebin(string file, Boolean showConfirmDialog = false)

{

SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();

shf.wFunc = FO_DELETE;

shf.fFlags = FOF_ALLOWUNDO;

if (!showConfirmDialog)

{

shf.fFlags |= FOF_NOCONFIRMATION;

}

shf.pFrom = file + '\0' + '\0';

SHFileOperation(ref shf);

}

#endregion

}

}

2.该方法转载太多,就找了个百度的链接了(http://zhidao.baidu.com/link?url=jnBCVYOf4yhGdf_YjDFZZQca1EsBixJ4LB1e8lmKotTMjrVI2StNMkswmSlQKUrIkA1_O9RMFsnUd9f_CYWTXa)

 

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Runtime.InteropServices;

  6.  
  7. namespace ceshiConsole

  8. {

  9. public class Cleaner

  10. {

  11. private const int FO_DELETE = 0x3;

  12. private const ushort FOF_NOCONFIRMATION = 0x10;

  13. private const ushort FOF_ALLOWUNDO = 0x40;

  14.  
  15. [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]

  16. private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);

  17.  
  18. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

  19. public class _SHFILEOPSTRUCT

  20. {

  21. public IntPtr hwnd;

  22. public UInt32 wFunc;

  23. public string pFrom;

  24. public string pTo;

  25. public UInt16 fFlags;

  26. public Int32 fAnyOperationsAborted;

  27. public IntPtr hNameMappings;

  28. public string lpszProgressTitle;

  29. }

  30.  
  31. public static int Delete(string path)

  32. {

  33. _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();

  34. pm.wFunc = FO_DELETE;

  35. pm.pFrom = path + '\0';

  36. pm.pTo = null;

  37. pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;

  38. return SHFileOperation(pm);

  39. }

  40. }

  41. }

以上在ceshiConsole项目中实现。

以上2中方法的主要区别在于,前者使用的是struct SHFILEOPSTRUCT,后者使用的是class _SHFILEOPSTRUCT。与VB中FileIO.FileSystem.DeleteFile的区别在于,CS调用删除时,如果出错(比如文件不存在,etc)时,不报Exception异常,而是通过SHFileOperation的返回值来区别错误(异常)类型的;而VB报异常。

附:

1.关于SHFileOperation函数的补充

2.C# 删除移动文件(附带弹窗和进度提示)

C#删除文件和文件夹到回收站

如果使用C#代码来删除文件或是文件夹。会将文件和文件夹直接删除,而不是删除到回收站。可以调用Microsoft.VisualBasic.dll提供的方法。

首先对项目添加名为Microsoft.VisualBasic.dll的引用,然后添加命名空间

using Microsoft.VisualBasic.FileIO;

最后示例代码如下:

using System;
using Microsoft.VisualBasic.FileIO;
names paceleaver{
    class Program{
 
        static void Main(string[]args) {
 
            Console.WriteLine("删除文件到回收站");
            stringfilepath="leaver.txt";
            FileSystem.DeleteFile(filepath,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin);
            Console.WriteLine("删除文件完成");
 
            Console.WriteLine("删除文件夹到回收站");
            stringdirpath="leaver";
            FileSystem.DeleteDirectory(dirpath,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin);
            Console.WriteLine("删除文件夹完成");
        }
    }
}

很简单。。就不多说了。。

你可能感兴趣的:(C#)