views:

52

answers:

2

Hello all,

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.

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 :-)

+1  A: 

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.

Greg D
LOL, 10x, thought about it myself and don't worry, i allready gave myself a punch....i was so stupid, just had to close the resource...your right...10x :-)
Erez
+3  A: 

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);
Musa Hafalır
Good idea, but the Flush and close are unnecessary as they are implicit in the Dispose()
erikkallen
you are right erikkallen. i am correcting it. thanks.
Musa Hafalır