1

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!

Sulaiman
  • 147
  • 4
  • 11
  • I don't see anything obviously wrong. What does `Directory.Exists()` return? Is your environment anything out of the ordinary, like a virtual machine? – Patrick Quirk Nov 23 '12 at 14:20
  • What is the actual `tesseractPath` computed value? – Steve B Nov 23 '12 at 14:21
  • .Exist returns a bool value (depending on whether it exists or not). And there is no VM. Though I just realized I should try testing the same method on a clean project... But there's nothing really wrong in this one either. – Sulaiman Nov 23 '12 at 14:25
  • @SteveB If I `console.writeline` it, this is what I get (kinda long): C:\Users\Sulaiman\Desktop\Programming\Visual Studio 2010\Projects\PDF_Image_to_Excel_Converter\Image-OCR\Atlasoft\TesseractLancher Files\tesseract And it does exist. I could copy/paste it into explorer and the folder opens. – Sulaiman Nov 23 '12 at 14:31
  • 1
    @Sulaiman.89 if your path is already that long, and you're running the code from four directories down from that, you are possibly hitting max path length issues. Win7 has a max of 260. Try moving your project to c:\MySweetProject\ and see if you get the same results. – drch Nov 23 '12 at 16:34
  • @Eren Ersönmez was right in my case. My IIS Web application (app domain which was run under iis user permissions) doesn't have permission to read folder files so that it return false becouse of exception of no permissions – Vladimir Shmidt Jun 19 '14 at 14:20

3 Answers3

3

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.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • Makes sense, but none of those exceptions apply to this case... I have full access and not using any invalid characters. – Sulaiman Nov 23 '12 at 14:28
0

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.

Pat
  • 1
0

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;
}
sunamo.cz
  • 59
  • 6