Jump to content
Tuts 4 You

C# .NET File.Open() strage behaviour


Recommended Posts

Let's say I have a piece of code like this one:

private void RecursePath(DirectoryInfo Root)
if (Root.Exists)
FileInfo[] files = Root.GetFiles();
LastFile = "";
foreach (FileInfo fi in files)
LastFile = fi.FullName;
FileStream f = File.Open(LastFile, FileMode.Open);
//FileIO TestFile = new FileIO(LastFile, FileIO.FileIoMode.OpenExisting);
catch (Exception e)
PrintException(LastFile, e);

Yeah, it's a bit ugly sorry. The point is that I'm having some troubles trying to understand why File.Open() throws PathTooLongException with paths shorter than 248/260 bytes (I'm talking about 160 bytes or so). After attaching a native debugger to the running app I can see that CreateFileW is returning a valid handle and GetLastErr = ERROR_SUCCESS. WTF!?

Also, if use CreateFileW directly using NativeMethods (that's what this commented FileIO class does) the behaviour is exactly the same.

So the question is... anyone experienced similar problems? Or has any hint about where's the problem? I know this is not strictly related to reversing but I think is strange enough to share this X-File ;D


Link to comment

Reply-to-myself. The problem was at LastFile = fi.FullName;

So only by accessing fi.FullName causes a PathTooLongException if FullName.Length > 260 and my code was showing LastFile.... bakayaro!! The funny thing is that in the MSDN doc says FullName can only throw SecurityException :blink:

Well, sorry about the noise :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...