Search code examples
asp.netdirectoryinfo

DirectoryInfo Class Exists false if a folder contains spaces between name


string ActualPath = "D:\Files\a             c"

DirectoryInfo di = DirectoryInfo(ActualPath);

The di.Exist is always false when a folder contains spaces between name... what is the problem in the code...where the directory is actualy exist.

Thanks in advance...


Solution

  • I don't think the problem is the spaces. I think that the problem is that you need to use the proper escape sequence for the backslashes in your path eg.

    string ActualPath = "D:\\Files\\a             c";
    

    OR

    string ActualPath = @"D:\Files\a             c";
    

    Try

    string ActualPath = @"D:\Files\a             c";
    DirectoryInfo di = new DirectoryInfo(ActualPath);   
    if (di.Exists)
    {
       //do something
    }