File.Exists() incorrectly returns false when path is too long

后端 未结 5 657
无人及你
无人及你 2020-12-06 10:48

I am currently working on a program that traverses through various directories to ensure that specific files are present by using File.Exists().

The app

5条回答
  •  渐次进展
    2020-12-06 10:59

    This is ugly and inefficient, but it DOES get around the MAX_PATH limitation:

    const int MAX_PATH = 260;
    
    private static void checkPath(string path)
    {
        if (path.Length >= MAX_PATH)
        {
            checkFile_LongPath(path);
        }
        else if (!File.Exists(path))
        {
            Console.WriteLine("   *  File: " + path + " does not exist.");
        }
    }
    

    And here is the checkFile_LongPath function:

    private static void checkFile_LongPath(string path)
    {
        string[] subpaths = path.Split('\\');
        StringBuilder sbNewPath = new StringBuilder(subpaths[0]);
        // Build longest subpath that is less than MAX_PATH characters
        for (int i = 1; i < subpaths.Length; i++)
        {
            if (sbNewPath.Length + subpaths[i].Length >= MAX_PATH)
            {
                subpaths = subpaths.Skip(i).ToArray();
                break;
            }
            sbNewPath.Append("\\" + subpaths[i]);
        }
        DirectoryInfo dir = new DirectoryInfo(sbNewPath.ToString());
        bool foundMatch = dir.Exists;
        if (foundMatch)
        {
            // Make sure that all of the subdirectories in our path exist.
            // Skip the last entry in subpaths, since it is our filename.
            // If we try to specify the path in dir.GetDirectories(), 
            // We get a max path length error.
            int i = 0;
            while(i < subpaths.Length - 1 && foundMatch)
            {
                foundMatch = false;
                foreach (DirectoryInfo subDir in dir.GetDirectories())
                {
                    if (subDir.Name == subpaths[i])
                    {
                        // Move on to the next subDirectory
                        dir = subDir;
                        foundMatch = true;
                        break;
                    }
                }
                i++;
            }
            if (foundMatch)
            {
                foundMatch = false;
                // Now that we've gone through all of the subpaths, see if our file exists.
                // Once again, If we try to specify the path in dir.GetFiles(), 
                // we get a max path length error.
                foreach (FileInfo fi in dir.GetFiles())
                {
                    if (fi.Name == subpaths[subpaths.Length - 1])
                    {
                        foundMatch = true;
                        break;
                    }
                }
            }
        }
        // If we didn't find a match, write to the console.
        if (!foundMatch)
        {
            Console.WriteLine("   *  File: " + path + " does not exist.");
        }
    }
    

提交回复
热议问题