Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve path information from PathTooLongException
    text
    copied!<p>I am using DirectoryInfo and FileInfo in .Net 4.0 to enumerate files in a directory tree and I am hitting PathTooLongException. Simplified version is below</p> <pre class="lang-cs prettyprint-override"><code>public static class Test { public static void Search(DirectoryInfo base) { foreach(var file in base.GetFiles()) { try { Console.WriteLine(file.FullName); } catch(PathTooLongException ex) { // What path was this? } } foreach(var dir in base.GetDirectories()) { Search(dir); } } } </code></pre> <p>When the error is thrown, I want to know what file path caused the problem. Obviously I can't ask for <code>FullName</code> as that is what errored. I can get name from <code>file.Name</code>, but if I can't get the rest of the path as <code>file.Directory</code> gives a <code>PathTooLongException</code> even though the <code>DirectoryInfo</code> that the file was found from worked fine! (I can't use that though as the actual code is much more complex).</p> <p>Looking through the stack trace it seems that it's using an internal path (I see a protected <code>file.FullPath</code> from debug), and trying to tear the directory from the full (oversized) path. Most of the problems seem to involve <code>System.IO.Path.NormalizePath</code>, with I hear went through a few changes in .Net 4.0. I have not tried on previous versions of the framework.</p> <p>My questions:</p> <ol> <li>How can I get the full path from this exception; it is seemingly passed without any useful information.</li> <li>Why would the framework need to limit characters in a path to chop off the filename?</li> </ol> <p>Thanks in advance for any help,<br> Andy</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload