Try and get the directory name e.g. Path.GetDirectoryName, then use Directory.GetCurrentDirectory, remember it in a variable then use Directory.SetCurrentDirectory to the directory then try using File.Exists via Path.GetFileName. So what you are doing is eliminating the directory name and searching just on the file name. When done use Directory.SetCurrentDirectory
C# File.Exists is not finding files
I have a small console app that gets a list of files and their paths from a database, then loops through the list checking to see if the file actually exists. For some reason "File.Exists" always returns false, even when the file does exist. Here's my code:
bool found = File.Exists(item.FilePath + item.FileName);
if (found == false)
{
LogMissingFilesV2(item.FileId.ToString(), item.FilePath, item.FileName);
}
else
{
Console.WriteLine(item.FileName);
}
found is always false.
and here's and example of a full path where the file does exists and found = false:
D:\Sites\TransAct\TA_054\FS\Agreement\dea4d6fa-d410-4324-9d7b-39840020d76c_7-ROHDE 2-1H ARO DOCUMENTS.pdf
To make sure the path is correct I find the file in File Explorer, copy its path and then name into notepad and compare. they are both exactly the same
Also, if I search this string in File Explorer:
dea4d6fa-d410-4324-9d7b-39840020d76c_7-ROHDE 2-1H ARO DOCUMENTS.pdf
File Explorer cannot find the file but if I search on:
dea4d6fa-d410-4324-9d7b-39840020d76c
I can find it. Its like the name is too complex.
How can it make this work as intended?
Thank you