如果文件被其他进程使用,如何删除? [英] How to delete a file if it is used by another process?

查看:82
本文介绍了如果文件被其他进程使用,如何删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我正在上传一个excel文件以将记录插入表格。我将文件保存在服务器上以插入记录,并且我的所有记录都已成功上传。上传文件后我想从保存的位置删除文件,但是当我试图删除文件时出现错误文件正在被另一个进程使用。



Hi I am uploading a excel file to insert the record into table. I am saving the file on server to insert the record and my all records are successfully uploaded. After the uploading the file i want to delete the file from saved location but when i trying to delete the file an error occur "The file is being used by another process".

try
{                   
   System.IO.File.Delete(tPath);
}
catch (System.IO.IOException ex)
{
   Console.WriteLine(ex.Message); 
}







请帮我怎么做?




Please help how i can do it?

推荐答案

您无法删除该文件的原因很充分。假设一个会计流程正在忙于从文件中读取以创建分类帐条目,并且您从其下方取出文件 - 突然之间分类帐将无法平衡。在这种情况下,Windows试图保护您。



如果您必须删除该文件,理论上您可以发出 DEL文件名命令(相当于在命令提示符下调用DEL)通过 Process.Start 。另一种方法是使用一个涉及大量p /调用的更复杂的过程来杀死文件上的锁。
You can't delete the file for very good reasons. Suppose that an accounting process is busy reading from the file to create ledger entries and you take the file out from underneath them - all of a sudden the ledger will not balance. In this case, Windows is trying to protect you.

If you must delete the file, then theoretically you could issue a DEL filename command (the equivalent of calling DEL at the command prompt) through Process.Start. The alternative is to kill the lock on the file using a much more complex process which involves a lot of p/invoking.


你做不到。当文件打开时,它被os锁定。你不能重命名,删除,移动该文件。据我了解,使用后不要关闭该文件。第一个关闭文件及其所有引用,然后你可以删除它。
you cannot. when a file is open, it is locked by os. you cannot rename, delete, move that file. As I understand, you don't close that file after using it. first close file and all references to it, then you can delete it.


你不能删除这些文件,但你可以照顾它。请参阅我过去的回答:

使用的文件另一个流程异常 [ ^ ]。



-SA
You cannot delete such files, but you can take care about it. Please see my past answer:
File used by another process exception[^].

—SA


这篇关于如果文件被其他进程使用,如何删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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