File.Exists()不正确时路径过长返回false [英] File.Exists() incorrectly returns false when path is too long

查看:194
本文介绍了File.Exists()不正确时路径过长返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个程序,遍历各种目录,以确保使用 File.Exists()

这是特定的文件是present

应用程序一直声称某些文件不存在时,他们实际上做的,我最近发现这个错误是由于道路太漫长。

我知道有上,这样的地址 File.Exists()返回不正确的值,但没有一个似乎解决这​​一具体问题。

问题

重命名的目录和文件,以缩短路径是不是一个真正的选择,所以我不知道该怎么做,在这一点上。有没有变通办法将解决这个问题?

在code中的使用是没有什么特别的(我剪了一些无关紧要的code),但我会包括在下面,以防万一它帮助。

 私人无效checkFile(字符串路径)
    {
        如果(!File.Exists(路径))
            Console.WriteLine(*文件:+路径+不存在。);
    }
 

解决方案

这是丑陋和低效的,但它周围的MAX_PATH限制得到:

  const int的MAX_PATH = 260;

私有静态无效的CheckPath(字符串路径)
{
    如果(path.Length> = MAX_PATH)
    {
        checkFile_LongPath(路径);
    }
    否则,如果(!File.Exists(路径))
    {
        Console.WriteLine(*文件:+路径+不存在。);
    }
}
 

这里是checkFile_LongPath功能:

 私有静态无效checkFile_LongPath(字符串路径)
{
    字符串[]的子路径= path.Split('\\');
    StringBuilder的sbNewPath =新的StringBuilder(子路径[0]);
    //构建最长的子路径小于MAX_PATH字符
    的for(int i = 1; I< subpaths.Length;我++)
    {
        如果(sbNewPath.Length +子路径[I] .Length> = MAX_PATH)
        {
            子路径= subpaths.Skip(ⅰ).ToArray();
            打破;
        }
        sbNewPath.Append(\\+子路径[I]);
    }
    DirectoryInfo的DIR =新的DirectoryInfo(sbNewPath.ToString());
    布尔foundMatch = dir.Exists;
    如果(foundMatch)
    {
        //确保所有在我们的路径中的子目录的存在。
        //跳过在子路径中的最后一项,因为它是我们的文件名。
        //如果我们试图指定dir.GetDirectories路径()
        //我们得到的最大路径长度错误。
        INT I = 0;
        而(I< subpaths.Length  -  1安培;&安培; foundMatch)
        {
            foundMatch = FALSE;
            的foreach(DirectoryInfo的子目录中dir.GetDirectories())
            {
                如果(subDir.Name ==子路径[I])
                {
                    //移动到下一个子目录
                    DIR =子目录;
                    foundMatch = TRUE;
                    打破;
                }
            }
            我++;
        }
        如果(foundMatch)
        {
            foundMatch = FALSE;
            现在,我们已经通过所有的子路径历程//,看看我们的文件是否存在。
            //再次,如果我们试图指定dir.GetFiles路径​​()
            //我们得到了最大路径长度错误。
            的foreach(FileInfo的网络dir.GetFiles())
            {
                如果(fi.Name ==子路径[subpaths.Length  -  1])
                {
                    foundMatch = TRUE;
                    打破;
                }
            }
        }
    }
    //如果我们没有找到匹配,写入控制台。
    如果(!foundMatch)
    {
        Console.WriteLine(*文件:+路径+不存在。);
    }
}
 

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

The application has been claiming that certain files do not exist when they actually do, and I recently discovered that this error was due to the path being too long.

I realize there are questions on SO that address File.Exists() returning incorrect values, but none seem to solve this particular issue.

Renaming the directories and files to shorten the path is not really an option, so I'm not sure what to do at this point. Is there a work-around that would solve this problem?

The code in use is nothing special (I've cut out some irrelevant code), but I will include it below just in case it helps.

    private void checkFile(string path)
    {
        if (!File.Exists(path))
            Console.WriteLine("   *  File: " + path + " does not exist.");
    }

解决方案

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.");
    }
}

这篇关于File.Exists()不正确时路径过长返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆