我在上传新文件时遇到删除重复文件的错误 [英] I'm getting error on deleting duplicate file while uploding new file

查看:167
本文介绍了我在上传新文件时遇到删除重复文件的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx") )
               {
                   string path = Server.MapPath("~/Content/"+excelfile.FileName);
                   if (System.IO.File.Exists(path))
                   {
                  System.IO.File.Delete(path);
                    //not working 
                   }
                       excelfile.SaveAs(path);










Exception Details: System.IO.IOException: The process cannot access the file 'D:\Projects\mastertest\Content\Excel07.xlsx' because it is being used by another process.





我尝试过:



if(System.IO.File.Exists (路径))

{

System.IO.File.Delete(路径);

}

excelfile .SaveAs(path);



What I have tried:

if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
excelfile.SaveAs(path);

推荐答案

常见错误是文件被其他用户打开或拥有(拒绝访问)而被锁定。



要获取错误原因,请捕获引发的异常并将其打印出来。另请参见 File.Delete方法(字符串)(系统.IO) [ ^ ]。
Common errors are that the file is locked by being opened or owned by a different user (acess denied).

To get the error reason catch the raised exception and print it out. See also File.Delete Method (String) (System.IO)[^].


在你开始疯狂追逐之前,我会抓住一份Sysinternals Process Explorer Process Explorer [ ^ ] - 你可以看到哪个进程使用该工具锁定文件(还有其他具有相同功能的工具,SysInternals的东西非常好)



一旦你知道什么进程持有文件,那么你可以考虑一个动作 - 你不要告诉我们你自己发生了什么事情处理我们能够帮助你更多
Before you go off on a wild goose chase, I'd grab a copy of Sysinternals Process Explorer Process Explorer[^] -- iirc you can see what process is locking the file using that tool (there are other tools with the same ability, the SysInternals stuff is mighty fine)

Once you know what process is holding the file, then you can think about an action - you dont tell us enough bout whats going on in your own processing for us to be able to help you much more


我找到了解决方案:

i have find solution :
workbook.Close();
  application.Quit();



需要关闭excel工作簿和应用程序


need to close the excel workbook and application


这篇关于我在上传新文件时遇到删除重复文件的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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