且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一旦进程完成并将其复制到目标,如何从源文件夹中删除文件?

更新时间:2023-01-30 20:00:26

你必须识别过程打开文件。



确定您可以处理的流程e Process Explorer [ ^ ](按Ctrl + F或打开查找菜单,选择文件句柄或DLL,然后输入文件名)。 />


如果是您的应用程序,只需确保文件已被删除,然后再删除它们。


System.GC.Collect() ; //强制所有代的立即垃圾收集。



System.GC.WaitForPendingFinalizers(); //将停止所有当前运行的线程



现在你可以删除你的文件和文件夹。如下所示



System.IO.DirectoryInfo di = new DirectoryInfo(你的路径);



foreach(di.GetFiles()中的FileInfo文件)

{

file.Delete();

}

foreach(在di.GetDirectories()中的DirectoryInfo目录)

{

dir.Delete(true);

}


hello everyone i have done with processing of files from source to destination folder but my intention is to delete the files in source folder after processing is complete.

but im getting the error like "The process cannot access the file 'C:\Datacap\APT\Images\Input\APT001.tif' because it is being used by another process."

What I have tried:

hello everyone i have done with processing of files from source to destination folder but my intention is to delete the files in source folder after processing is complete.

but im getting the error like "The process cannot access the file 'C:\Datacap\APT\Images\Input\APT001.tif' because it is being used by another process."

You have to identify the process that has the file opened.

To identify the processes you can use the Process Explorer[^] (press Ctrl+F or open the Find menu, select file handle or DLL, and enter the file name).

If it is your application, just ensure that the files have been closed before deleting them.


System.GC.Collect();// Forces an immediate garbage collection of all generations.

System.GC.WaitForPendingFinalizers();// will stops all the current running threads

now you can delete your files and folders..like below

System.IO.DirectoryInfo di = new DirectoryInfo("your path");

foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}