Untitled
unknown
plain_text
a year ago
2.8 kB
3
Indexable
Never
public static bool 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-1; i++) { if (sbNewPath.Length + subpaths[i].Length >= 250) //260 max characters in local system { 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; } } } } bool found = false; // If we didn't find a match, write to the console. if (!foundMatch) { Console.WriteLine(" * File: " + path + " does not exist."); return false; } else if (foundMatch) { found = true; return found; } return found; }