c# 如何将文件删除到回收站(文件操作)
2014-05-24 01:12
453 查看
很多一部分网上回答都是调用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)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ceshiConsole
{
public class Cleaner
{
private const int FO_DELETE = 0x3;
private const ushort FOF_NOCONFIRMATION = 0x10;
private const ushort FOF_ALLOWUNDO = 0x40;
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class _SHFILEOPSTRUCT
{
public IntPtr hwnd;
public UInt32 wFunc;
public string pFrom;
public string pTo;
public UInt16 fFlags;
public Int32 fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
public static int Delete(string path)
{
_SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
pm.wFunc = FO_DELETE;
pm.pFrom = path + '\0';
pm.pTo = null;
pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
return SHFileOperation(pm);
}
}
}
以上在ceshiConsole项目中实现。
以上2中方法的主要区别在于,前者使用的是struct SHFILEOPSTRUCT,后者使用的是class _SHFILEOPSTRUCT。与VB中FileIO.FileSystem.DeleteFile的区别在于,CS调用删除时,如果出错(比如文件不存在,etc)时,不报Exception异常,而是通过SHFileOperation的返回值来区别错误(异常)类型的;而VB报异常。
附:
1.关于SHFileOperation函数的补充
2.C#
删除移动文件(附带弹窗和进度提示)
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)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ceshiConsole
{
public class Cleaner
{
private const int FO_DELETE = 0x3;
private const ushort FOF_NOCONFIRMATION = 0x10;
private const ushort FOF_ALLOWUNDO = 0x40;
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class _SHFILEOPSTRUCT
{
public IntPtr hwnd;
public UInt32 wFunc;
public string pFrom;
public string pTo;
public UInt16 fFlags;
public Int32 fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
public static int Delete(string path)
{
_SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
pm.wFunc = FO_DELETE;
pm.pFrom = path + '\0';
pm.pTo = null;
pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
return SHFileOperation(pm);
}
}
}
以上在ceshiConsole项目中实现。
以上2中方法的主要区别在于,前者使用的是struct SHFILEOPSTRUCT,后者使用的是class _SHFILEOPSTRUCT。与VB中FileIO.FileSystem.DeleteFile的区别在于,CS调用删除时,如果出错(比如文件不存在,etc)时,不报Exception异常,而是通过SHFileOperation的返回值来区别错误(异常)类型的;而VB报异常。
附:
1.关于SHFileOperation函数的补充
2.C#
删除移动文件(附带弹窗和进度提示)
相关文章推荐
- C# 文件操作(上传 下载 删除 文件列表...)
- 在C#中如何将文件放入回收站。
- C# 文件操作(上传 下载 删除 文件列表...)
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- C#引用Microsoft.VisualBasic将目录或文件删除到回收站。
- C# 文件操作(上传 下载 删除 文件列表...)
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- C# 文件操作(上传 下载 删除 文件列表...)(转)
- C# 文件操作(上传 下载 删除 文件列表...)
- C# 文件操作(上传 下载 删除 文件列表...)
- C# 文件操作(上传 下载 删除 文件列表...)
- Asp.net(C#)对文件操作的方法(读取,删除,批量拷贝,删除...)
- 如何在客户端通过MSSQL执行某操作删除服务器的指定文件
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- C# 文件操作(上传 下载 删除 文件列表...)