Reputation: 147
As title explains, I have a program that checks if a directory exists before continuing.
And when the check is made it, it says the directory doesn't exist when it does!
Here's the code for storing the directory path:
string currentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
Console.WriteLine("----" + currentDirectory.ToString());
string tesseractPath = Path.Combine(currentDirectory, @"..\..\..\tesseract");
_wrapper = new AsyncTesseractWrapper(tesseractPath);
public TesseractWrapper(string programLoc)
{
DirectoryInfo dinfo = new DirectoryInfo(programLoc);
//DirectoryInfo dinfo = new DirectoryInfo("C:\\Windows");
ValidateTesseractDirectory(dinfo);
_tesseractLocation = dinfo.FullName;
}
And the code for performing the check:
private void ValidateTesseractDirectory(DirectoryInfo dinfo)
{
if (!dinfo.Exists)
throw new ArgumentException("Specified program directory must exist.");
FileInfo[] files;
files = dinfo.GetFiles(_tessExe);
if (files.Length != 1)
throw new ArgumentException("Specified program directory must contain tesseract.exe.");
}
I've tried debugging with several variations, like checking if the C:\Windows folder exists and it's still giving me an error...
Is there something wrong with the code, or my understanding of the .Exists method... ?
Thanks!
Upvotes: 1
Views: 6358
Reputation: 69
Experienced actually the same. It was due to use soft link (text file with information about folder) instead of junction.
Load recursively first folder, then their files and repair wrong junction. long file names etc.
public static List<string> GetFilesEveryFolder(string folder, string mask, SearchOption searchOption, bool _trimA1 = false)
{
List<string> list = new List<string>(); ;
List<string> dirs = null;
try
{
dirs = GetFoldersEveryFolder(folder, "*").ToList();
}
catch (Exception ex)
{
throw new Exception("GetFiles with path: " + folder, ex);
}
foreach (var item in dirs)
{
try
{
list.AddRange(Directory.GetFiles(item, mask, SearchOption.TopDirectoryOnly));
}
catch (Exception ex)
{
// Not throw exception, it's probably Access denied on Documents and Settings etc
//ThrowExceptions.FileSystemException(type, RH.CallingMethod(), ex);
}
}
CA.ChangeContent(list, d => SH.FirstCharLower(d));
if (_trimA1)
{
list = CA.ChangeContent(list, d => d = d.Replace(folder, ""));
}
return list;
}
Upvotes: 0
Reputation: 1
I think the problem is that Microsoft has changed the structures of folders and 'obviously' their staff are still looking at 'the good old way'. In the past a folder used to have '..' which was the 'folder mark' (dos of course) I can see this does not exist any more. What I did: I put/copy a dummy file into a new folder, an image or whatever and instead of using 'directory' I use file.exists I think the answer might be in the attributes.
Upvotes: 0
Reputation: 39095
It might be because of a permissions issue. To quote MSDN:
The Exists property returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.
Upvotes: 3