开发者

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 f开发者_JAVA技巧inished 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.

p.s. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜