///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);///implementationpublic bool DeleteFiles(string[] files){ SHFILEOPSTRUCT FileOpStruct = new SHFILEOPSTRUCT();
///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);///implementationpublic 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;
}
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);
} 调用这个delete方法
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(String, UIOption, RecycleOption):注意:此方法在 .NET Framework 2.0 版中是新增的。 删除文件。 命名空间:Microsoft.VisualBasic.FileIO
程序集:Microsoft.VisualBasic(在 microsoft.visualbasic.dll 中)
参数
file
String。要删除的文件的名称和路径。必选。 showUI
UIOption。是否对操作进度进行可视跟踪。默认为 UIOption.OnlyErrorDialogs。必选。 recycle
RecycleOption。删除的文件是否应发送到“回收站”。默认为 RecycleOption.DeletePermanently。必选。