Delete File

  • Thread starter Thread starter Ryan Moore
  • Start date Start date
R

Ryan Moore

In an asp.net app, I'm trying to do some file upload / deleting for an image
gallery.

Deleting an image works great, except right after I have uploaded the file -
I cannot seem to get file access.

What do I need to do to solve this problem?

public bool DeleteImage(string fileName){

if (File.Exists(fileName))

{

try

{

if (this.WaitForExculsiveAccess(fileName))

{

System.IO.File.Delete(fileName);

return true;

}

else

{

return false;

}

}

catch (Exception e3)

{

return false;

}

}

else

{

return false;

}

}

private bool WaitForExculsiveAccess(string FullPath)

{

int ccount=0;

while (true)

{

if (ccount<5)

{

try

{

FileStream fs = new FileStream(FullPath, FileMode.Append, FileAccess.Write,
FileShare.None);

fs.Close();

return true;

}

catch

{

ccount++;

Thread.Sleep(1000);

}

}

else

{

return false;

}

}

}
 
Is it possible that the file hasn't finished being uploaded before you
try to delete it? It depends on the details of how you're uploading the
file. I've had that problem with file uploads in the past when I have
used file system notification to detect the presence of a new file.

Bruce Johnson
http://www.ObjectSharp.com/Bruce
 
I've tried 2 different methods of uploading the file and it doesn't seem to
make a difference. what is file system notificaion?
 
File system notification is just a way of having an event raised when
something (like the addition of a file) happens to the file system.
Probably doesn't have bearing on what you're doing.

If I were in your situation, I'd be looking at the upload code to see
exactly what is happened to the file (such as, when is it being closed)
especially in relation to when you are trying to delete the file. Would
it be possible to post the upload code or provide some idea of what it's
doing and then the delete is called?

Bruce Johnson
http://www.ObjectSharp.com/Bruce
 
Back
Top