一旦进程完成并将其复制到目标,如何从源文件夹中删除文件? [英] How to delete files form source folder once process is complete and it is copied to destination?

查看:121
本文介绍了一旦进程完成并将其复制到目标,如何从源文件夹中删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我已经处理过从源到目标文件夹的文件,但我的目的是在处理完成后删除源文件夹中的文件。



但是即时收到错误,如进程无法访问文件'C:\Datacap \ APT \Images \输入\ APT001.tif',因为它正被另一个进程使用。



我尝试了什么:



大家好我处理从源到目的地的文件文件夹,但我的目的是在处理完成后删除源文件夹中的文件。



但我得到的错误如进程无法访问文件C:\\ \\Datacap\APT\Images\Input\APT001.tif,因为它正被另一个进程。

解决方案
你必须识别过程打开文件。



确定您可以处理的流程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);
}


这篇关于一旦进程完成并将其复制到目标,如何从源文件夹中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆