C# 如何快速删除多个文件

C# 如何快速删除多个文件,c#,directory,C#,Directory,我在Windows Server中有一个文件夹,其中包含子文件夹和≈50000个文件。当我单击鼠标右键并选择删除(或shift+delete)时,所有文件都会在10-20秒内删除 当我使用代码删除文件时–1500-4000秒 -不要为我工作 我的代码: string folderPath = @"C://myFolder"; DirectoryInfo folderInfo = new DirectoryInfo(folderPath); folderInfo.Delete(

我在Windows Server中有一个文件夹,其中包含子文件夹和≈50000个文件。当我单击鼠标右键并选择删除(或shift+delete)时,所有文件都会在10-20秒内删除

当我使用代码删除文件时–1500-4000秒

-不要为我工作

我的代码:

string folderPath = @"C://myFolder";
DirectoryInfo folderInfo = new DirectoryInfo(folderPath);
folderInfo.Delete(true); // true - recursive, with sub-folders

如何更快地删除文件?

删除文件的更快方法是使用Windows函数而不是.NET函数

您需要首先导入函数:

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteFile(string lpFileName);
然后你可以这样做:

string[] files = Directory.EnumerateFiles(path, "*". SearchOption.AllDirectories);

foreach (string file in files)
{
    DeleteFile(file);
}

一旦文件被删除(这是使用托管API最慢的部分),您可以调用
Directory.DeleteFolder(path,true)
删除空文件夹。

删除文件的更快方法是使用Windows函数而不是.NET函数

您需要首先导入函数:

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteFile(string lpFileName);
然后你可以这样做:

string[] files = Directory.EnumerateFiles(path, "*". SearchOption.AllDirectories);

foreach (string file in files)
{
    DeleteFile(file);
}

一旦文件被删除,这是使用托管API最慢的部分,您可以调用
Directory.DeleteFolder(path,true)
来删除空文件夹。

因为问题实际上是关于删除网络共享文件夹的,而且据说基于浏览器的删除比C#内部删除机制快得多,只调用基于windows shell的删除可能会有所帮助

ProcessStartInfo Info = new ProcessStartInfo(); 
Info.Arguments = "/C rd /s /q \"<your-path>\""; 
Info.WindowStyle = ProcessWindowStyle.Hidden; 
Info.CreateNoWindow = true; 
Info.FileName = "cmd.exe"; 
Process.Start(Info);
ProcessStartInfo Info=newprocessstartinfo();
Info.Arguments=“/C rd/s/q\”;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName=“cmd.exe”;
进程启动(信息);
当然,您必须替换


但是,我现在没有可以自己测试性能的基础结构和文件。

因为问题实际上是关于删除网络共享文件夹的,而且据说基于资源管理器的删除比C#内部删除机制快得多,所以只调用基于windows shell的删除可能会有所帮助

ProcessStartInfo Info = new ProcessStartInfo(); 
Info.Arguments = "/C rd /s /q \"<your-path>\""; 
Info.WindowStyle = ProcessWindowStyle.Hidden; 
Info.CreateNoWindow = true; 
Info.FileName = "cmd.exe"; 
Process.Start(Info);
ProcessStartInfo Info=newprocessstartinfo();
Info.Arguments=“/C rd/s/q\”;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName=“cmd.exe”;
进程启动(信息);
当然,您必须替换


但是,我现在没有可以自己测试性能的基础结构和文件。

不太清楚为什么在删除包含大量文件和子文件夹的文件夹时,DirectoryInfo.Delete()方法会花费太多时间。我怀疑这个方法可能也会做很多不必要的事情

我编写了一个小类来使用WinAPI,而不用做太多不必要的事情来测试我的想法。删除包含50000个文件和子文件夹的文件夹大约需要40秒。所以,希望它能有所帮助

我使用此PowerScript生成测试文件

$folder = "d:\test1";
For ($i=0; $i -lt 50000; $i++)
{
    New-Item -Path $folder -Name "test$i.txt" -ItemType "file" -Value $i.ToString();
}
下面是C#中的代码

使用系统;
使用System.Collections.Generic;
//
使用System.Runtime.InteropServices;
使用System.IO;
//
命名空间TestFileDelete
{
类文件删除
{
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
结构WIN32\u查找\u数据w
{
公共文件属性dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public UInt32 nFileSizeHigh;//DWORD
public UInt32 nFileSizeLow;//DWORD
公共UInt32 dwReserved0;//DWORD
公共UInt32 dwReserved1;//DWORD
[Marshallas(UnmanagedType.ByValTStr,SizeConst=260)]
公共字符串cFileName;
[Marshallas(UnmanagedType.ByValTStr,SizeConst=14)]
公共字符串cAlternateFileName;
};
静态只读IntPtr无效\u句柄\u值=新IntPtr(-1);
[DllImport(“kernel32.dll”,CharSet=CharSet.Unicode,SetLastError=true)]
私有静态外部IntPtr FindFirstFileW(字符串lpFileName,out WIN32_FIND_DATAW lpFindFileData);
[DllImport(“kernel32.dll”,CharSet=CharSet.Unicode,SetLastError=true)]
私有静态外部布尔FindNextFileW(IntPtr hFindFile,out WIN32_FIND_DATAW lpFindFileData);
[DllImport(“kernel32.dll”)]
私有静态外部布尔FindClose(IntPtr句柄);
[DllImport(“kernel32.dll”,CharSet=CharSet.Unicode,SetLastError=true)]
公共静态外部布尔DeleteFileW(字符串lpFileName);//删除现有文件
[DllImport(“kernel32.dll”,CharSet=CharSet.Unicode,SetLastError=true)]
private static extern Boolean removeddirectoryw(字符串lpPathName);//删除现有的空目录
//此方法检查给定文件夹是否为空。
公共静态布尔IsEmptyFolder(字符串文件夹)
{
布尔res=真;
if(folder==null&&folder.Length==0)
{
抛出新异常(文件夹+“无效”);
}
WIN32_FIND_DATAW findFileData;
字符串searchFiles=folder+@“\*.*”;
IntPtr searchHandle=FindFirstFileW(searchFiles,out findFileData);
if(searchHandle==无效的\u句柄\u值)
{
抛出新异常(“无法检查文件夹”+文件夹);
}
做
{
if((findFileData.dwFileAttributes&FileAttributes.Directory)==FileAttributes.Directory)
{
//找到一个子文件夹
if(findFileData.cFileName!=“&&findFileData.cFileName!=”)
{
res=假;
打破
}
}//如果((findFileData.dwFileAttributes&FileAttributes.Directory)=FileAt