I have a folder G:\Images which allows me to insert images from upload page via c#, problem when I go to delete a image from folder I get the following error.
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
If a user wants to update their profile picture, they upload the image, I then check the database for old image and then use code below to delete it from folder before adding new image id.
But if I upload new image and then upload image again straight after the error occurs.
My code is
if (System.IO.File.Exists(@"G:\\Images\\" + string.Format("{0}.png", OldProfileImage)))
{
try
{
System.IO.File.Delete(@"G:\\Images\\" + string.Format("{0}.png", OldProfileImage));
}
catch(Exception e)
{
logger.Error(string.Format("Delete file error Exception is {0} {1}", e.Source.ToString(), e.StackTrace.ToString()));
}
}
-----------------------As requested changed e.tostring()----------------
Error is: System.UnauthorizedAccessException: Access to the path 'G:\Images\5bb188f0-2508-4cbd-b83d-9a5fe5914a1b.png' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
But as stated above, i can insert, delete, but if i then insert, delete straight after error occurs.
e.ToString()
to let .net tell you what's wrong – Alex