Search code examples
c#file-exists

File.Exists is returning false even though I can see the file C#


Issue


I have a function which lets the user edit an image when they do this I save this new image to a file to which they I save to the database etc ...

The issue comes as when I try and look for the file I just saved it says it does not exist but it does?

Code


Here I am saving the new file to the TEMP folder:

string newFullTempFolderURL = Path.Combine(Global.TempFolder, newFullFileName + ".png");

_image.Save(newFullTempFolderURL, System.Drawing.Imaging.ImageFormat.Png);

At this point when I check the folder the file is in the folder with the new image.

Then when I go on to uploading the file to the server (Using BITS) I do a check to make sure the file exists:

if (File.Exists(Path.Combine(Global.TempFolder + "\\" + newFullFileName)))
{

}

This then returns false (Not exists) when i can see the file with my own eyes!

Anyone had this same issue?

EDIT1:

newFullFileName already contains .png:

string newFullFileName = string.Format(oldFileName.Substring(0, oldFileName.IndexOf("_") + 1) + DateTime.Now.ToString(), "yyyyMMddhhmmss").Replace(@"/", "").Replace(" ", "").Replace(":", "") + ".png";

Solution

  • your File.Exists does not contain the file extension as you manually added it when creating "newFullTempFolderURL". You need to append ".png" to the File.Exists check or better yet use File.Exists(newFullTempFolderURL); as it's already been pre-made.

    EDIT1: You are adding ".png" a second time. This is wrong, as the file created is ".png.png", and then you are checking to see if ".png" exists.