简体   繁体   中英

How do delete a files that was just uploaded to server and is still locked by the server?

My system Uploads files to an IIS server and then i manipualate them and after finished doing every thing, i need to delete the original file, the problem is that the IIS "grabs" the file and i can't delete it, when i close the procces i can delete it manualy from the server, but until i close the procces i can't even do that...

I'm using: File.Delete(Server.MapPath(OriginalFileVirtualPath)); to delete the file.

ps for now i'm using the .net server, not a full active IIS, but i don't think that the problem is over there...It even make some sence that when i upload a file to the sever, it won't let to delete it strate away, but i'm sure there is a work around...

10x :-)

You just need to close the File Stream. After that you can use File.Delete() method.

Doing the manipulations inside using () { } block and doing deletion after using block is a good practice:

using (FileStream stream = File.Open(filePath, FileMode.Open))
{
    // Manipulation stuff
}
File.Delete(filePath);

Do you remember to close the file after you're done manipulating it?

If the file is still open when you try to delete it, it may fail. (I'd have to double-check the docs). But failing to close the file will certainly cause the symptom you're observing where IIS sees the file as locked until you shut down your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM