Problem moving/deleting file after SaveAs()

  • Thread starter Thread starter Dylan Parry
  • Start date Start date
D

Dylan Parry

Hi,

I'm trying to upload some files via a form, and I have managed so far to
save the files on the server in a temporary directory using:

HttpPostedFile.SaveAs(string filename);

This is a necessary stage as I need to perform some tests on the file
before allowing it to be saved to its final destination directory. The
files being uploaded contain data, so it's necessary to test the
integrity of the data after uploading it.

The problem now is that, after I have uploaded and tested the data, I
then want to move the file (or delete it if it was not valid) to the
data directory. I then receive the following error:

"The process cannot access the file because it is being used by
another process."

I have tried to find a method of closing the file that has been saved by
the above method, but I can't seem to find one. Any suggestions?
 
Dylan Parry said:
I'm trying to upload some files via a form, and I have managed so far to
save the files on the server in a temporary directory using:

HttpPostedFile.SaveAs(string filename);

This is a necessary stage as I need to perform some tests on the file
before allowing it to be saved to its final destination directory. The
files being uploaded contain data, so it's necessary to test the
integrity of the data after uploading it.

The problem now is that, after I have uploaded and tested the data, I
then want to move the file (or delete it if it was not valid) to the
data directory. I then receive the following error:

"The process cannot access the file because it is being used by
another process."

I have tried to find a method of closing the file that has been saved by
the above method, but I can't seem to find one. Any suggestions?

Reflector indicates that that method does indeed close the file. You
might consider copying the data out of the HttpPostedFile.InputStream
and testing that directly, followed by saving to its final location
directly.

-- Barry
 
Hi,

Are you closing the file after y ou finish working with it, are you using a
thread or something like that?
Apparently that is the error you are getting

Another possible source of error (not present here) is if you want to move
the file to a dir to which the user under the IIS is executing has no
permission, by default ASP.net run under anonymous_IIS , this use has a very
limited set of permissions.
 
Pondering the eternal question of "Hobnobs or Rich Tea?", Ignacio Machin
( .NET/ C# MVP ) finally proclaimed:
Are you closing the file after y ou finish working with it, are you using a
thread or something like that?

I think you might be on to something there. I will check to see whether
the checking process actually closes the files - I have a feeling that
it might not!

Thanks,
 
Hi,

Reflector indicates that that method does indeed close the file. You
might consider copying the data out of the HttpPostedFile.InputStream
and testing that directly, followed by saving to its final location
directly.

She did test the data form the file, this mean she opened/read and not quite
close it :)


OP: Can you post the code you are using?
 
Pondering the eternal question of "Hobnobs or Rich Tea?", Ignacio Machin
( .NET/ C# MVP ) finally proclaimed:
She did test the data form the file, this mean she opened/read and not quite
close it :)

That's exactly what was happening... although I'm not sure who "she" is
:s
OP: Can you post the code you are using?

No need. I found that the method I was using to check the data (one that
I didn't write) wasn't closing the files on its own and needed me to
explicitly tell it to do so. As such it's working fine now!

Thanks,
 
Back
Top