使用asp.net动态删除文件夹中的文件 [英] Deleting the files within the folder dynamically using asp.net

查看:83
本文介绍了使用asp.net动态删除文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam正在使用报告的电子邮件功能.我的项目中有近10个报告.我的项目中有一个名为``reports''的文件夹.当用户从我的网页发送带有附件(pdf格式)的邮件时,该附件也应自动保存在``reports''文件夹中.可以正常工作.但是,每当用户尝试再次发送相同的报告时,我都希望删除"reports"文件夹中存在的较早版本,而当前报告应放在该文件夹中.

但显示如下错误.

The process cannot access the file ''E:\Pradeep\Current Projects\SprintSafety\reports\Zachry-ConsumableReport.pdf'' because it is being used by another process.

我的代码如下,用于删除操作.

Iam Working upon email functionality for reports.I have nearly 10 reports in my project. I have a folder with name ''reports'' in my project.When ever user sends the mail from my web page with attachements(in pdf format), automatically that particular attachment also should be saved within the ''reports'' folder.It''s fine working.But whenever user trying to send the samereport again,I would like to delete the earlier one that present within ''reports'' folder and the current report should be placed there.

but it''s showing error like below..

The process cannot access the file ''E:\Pradeep\Current Projects\SprintSafety\reports\Zachry-ConsumableReport.pdf'' because it is being used by another process.

my code as follows for delete operation..

File.Delete(Server.MapPath("reports") + "\\" + ASPxDropDownEdit1.SelectedItem.Text + "-ConsumableReport.pdf"); 


有什么主意吗?

谢谢你!
Pradeep


any idea?

thank u!
A Pradeep

推荐答案

您收到此错误,无法删除其他进程使用的文件.为避免此问题,每当对此文件执行操作时,请关闭文件句柄.
在同一问题上有很好的链接

看看
You have got this error, you cannot delete a file which is used by some another process. To avoid this issue, whenever you are doing something with this file, Close the file handle.
There is a very good link on the same issue

have a look


错误说明了一切.仍然有人打开了文件.

删除之前,您必须检查是否可以独家打开文件.

您的代码可能是:

the error says it all. Someone still has the file opened.

before you delete you have to check if you can open the file exclusivly.

your code could be:

try
                   {
                       // check if the file can be opened exclusive. If that is possible, then the file is found otherwise in transfer
                       using (stream = File.Open(Server.MapPath("reports") + "\\" + ASPxDropDownEdit1.SelectedItem.Text + "-ConsumableReport.pdf", FileMode.Open, FileAccess.Write, FileShare.None))
                       {
                           if (stream.CanWrite)
                               File.Delete(Server.MapPath("reports") + "\\" + ASPxDropDownEdit1.SelectedItem.Text + "-ConsumableReport.pdf");

                       }
                   }
                   catch (Exception err)
                   {
// file cannot be opened exclusively so it cannot be deleted

                   }


您可以找出哪个进程持有文件句柄,并通过System.Diagnostics.Process.Kill杀死该进程. 不推荐.如果您可以访问其源代码,则最好尝试解决该问题.

—SA
You can find out which process is holding a file handle and kill this process by System.Diagnostics.Process.Kill. Not recommended. Better try to fix offending process, if you have an access to its source code.

—SA


这篇关于使用asp.net动态删除文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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