PC Review


Reply
Thread Tools Rate Thread

Apparent bugs in System.IO.File.Exists and System.IO.FileInfo path handling

 
 
brant.usenet@gmail.com
Guest
Posts: n/a
 
      11th Jan 2005
Forgive me if this has already been covered; I've searched and found
nothing related.

File.Exists() seems to clean up the path passed into its path
parameter. Specifically, it seems to 'fix' any directory names within
the path with leading or trailing spaces.

E.g. assuming a file exists at "c:\foo\bar.txt"
> File.Exists(@"C:\foo\bar.txt); --> true (OK)
> File.Exists(@"C:\foo \bar.txt); --> true (incorrect)


Likewise, the FileInfo class constructor seems to do the same cleanup:

E.g. assuming a file exists at "c:\foo\bar.txt"
> FileInfo f1 = new FileInfo(@"C:\foo\bar.txt");
> FileInfo f2 = new FileInfo(@"C:\foo \bar.txt");
> f1.Exists --> true (OK)
> f1.FullName --> "C:\foo\bar.txt" (OK)
> f2.Exists --> true (no)
> f2.FullName --> "C:\foo\bar.txt" (!!!)


This is all very accomodating, but when testing for existance of a file
at a particular path, one would almost certainly prefer a more literal
result. Or, to at least have an option for such a literal test.

There are similar issues with Directory.CreateDirectory(). It will
silently fix up one's path and return success, leaving the caller to
believe that their (admittedly bogus) path was OK.

Can anyone tell me authoritatively that this is not by design? If it is
somehow by design, by what logic? How should I perform the test I
desire?

 
Reply With Quote
 
 
 
 
Norman Yuan
Guest
Posts: n/a
 
      11th Jan 2005
Interesting.

But, try this:

In Window Explorer, try to rename a folder name by appending a few "SPACE"
at the end of a folder name. Windows will alway trim off the SPACEs at the
end.

Also, if you set Windows Explorer to hide file name extension of known file
type, when you add SPACEs at the end of the file name, they get trimed
automatically, too; while if the file extension is shown, you can append
SPACE the the end of file name (before the extension).

I guess, this behaviour of Windows is by design, not a .NET bug. At least
when .NET runs on Windows platform.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Forgive me if this has already been covered; I've searched and found
> nothing related.
>
> File.Exists() seems to clean up the path passed into its path
> parameter. Specifically, it seems to 'fix' any directory names within
> the path with leading or trailing spaces.
>
> E.g. assuming a file exists at "c:\foo\bar.txt"
> > File.Exists(@"C:\foo\bar.txt); --> true (OK)
> > File.Exists(@"C:\foo \bar.txt); --> true (incorrect)

>
> Likewise, the FileInfo class constructor seems to do the same cleanup:
>
> E.g. assuming a file exists at "c:\foo\bar.txt"
> > FileInfo f1 = new FileInfo(@"C:\foo\bar.txt");
> > FileInfo f2 = new FileInfo(@"C:\foo \bar.txt");
> > f1.Exists --> true (OK)
> > f1.FullName --> "C:\foo\bar.txt" (OK)
> > f2.Exists --> true (no)
> > f2.FullName --> "C:\foo\bar.txt" (!!!)

>
> This is all very accomodating, but when testing for existance of a file
> at a particular path, one would almost certainly prefer a more literal
> result. Or, to at least have an option for such a literal test.
>
> There are similar issues with Directory.CreateDirectory(). It will
> silently fix up one's path and return success, leaving the caller to
> believe that their (admittedly bogus) path was OK.
>
> Can anyone tell me authoritatively that this is not by design? If it is
> somehow by design, by what logic? How should I perform the test I
> desire?
>



 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      11th Jan 2005
Offhand I'd imagine that this is a built-in behaviour to help stop
cononicalization attacks.

--
Sean Hederman

http://codingsanity.blogspot.com

"Norman Yuan" <(E-Mail Removed)> wrote in message
news:ftIEd.62597$nN6.21503@edtnps84...
> Interesting.
>
> But, try this:
>
> In Window Explorer, try to rename a folder name by appending a few "SPACE"
> at the end of a folder name. Windows will alway trim off the SPACEs at the
> end.
>
> Also, if you set Windows Explorer to hide file name extension of known
> file
> type, when you add SPACEs at the end of the file name, they get trimed
> automatically, too; while if the file extension is shown, you can append
> SPACE the the end of file name (before the extension).
>
> I guess, this behaviour of Windows is by design, not a .NET bug. At least
> when .NET runs on Windows platform.
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Forgive me if this has already been covered; I've searched and found
>> nothing related.
>>
>> File.Exists() seems to clean up the path passed into its path
>> parameter. Specifically, it seems to 'fix' any directory names within
>> the path with leading or trailing spaces.
>>
>> E.g. assuming a file exists at "c:\foo\bar.txt"
>> > File.Exists(@"C:\foo\bar.txt); --> true (OK)
>> > File.Exists(@"C:\foo \bar.txt); --> true (incorrect)

>>
>> Likewise, the FileInfo class constructor seems to do the same cleanup:
>>
>> E.g. assuming a file exists at "c:\foo\bar.txt"
>> > FileInfo f1 = new FileInfo(@"C:\foo\bar.txt");
>> > FileInfo f2 = new FileInfo(@"C:\foo \bar.txt");
>> > f1.Exists --> true (OK)
>> > f1.FullName --> "C:\foo\bar.txt" (OK)
>> > f2.Exists --> true (no)
>> > f2.FullName --> "C:\foo\bar.txt" (!!!)

>>
>> This is all very accomodating, but when testing for existance of a file
>> at a particular path, one would almost certainly prefer a more literal
>> result. Or, to at least have an option for such a literal test.
>>
>> There are similar issues with Directory.CreateDirectory(). It will
>> silently fix up one's path and return success, leaving the caller to
>> believe that their (admittedly bogus) path was OK.
>>
>> Can anyone tell me authoritatively that this is not by design? If it is
>> somehow by design, by what logic? How should I perform the test I
>> desire?
>>

>
>



 
Reply With Quote
 
brant.usenet@gmail.com
Guest
Posts: n/a
 
      11th Jan 2005
That's fine & dandy for Explorer to do, but if one calls
System.IO.File.Exists(path), one should be told if a file exists at
'path'. Anything more is second guessing the caller. At the *very
least*, if it is indeed intentional, this behavior should be documented.

 
Reply With Quote
 
Adam Tatusko, MCSD .NET, MCAD .NET, MCDBA, MCSE, M
Guest
Posts: n/a
 
      11th Jan 2005
I'd report this as a bug on MSDN at:
http://lab.msdn.microsoft.com/productfeedback/

 
Reply With Quote
 
brant.usenet@gmail.com
Guest
Posts: n/a
 
      12th Jan 2005
Thank you Adam, I'll do that and post any interesting results back
here.
Best,
Brant

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove .extension from a System.IO.FileInfo file ? gamesforums@hotmail.com Microsoft ASP .NET 3 3rd Oct 2007 12:16 PM
System.IO.File.Exists Method =?Utf-8?B?TWlrZQ==?= Microsoft VB .NET 4 22nd Mar 2007 10:56 AM
System.IO.File.Exists - what if network path and not C:\ Ronald S. Cook Microsoft C# .NET 5 9th Mar 2007 02:01 AM
System.IO.File.Exists raibeart Microsoft VB .NET 4 8th Feb 2006 03:02 PM
Re: System.IO.Directory / System.IO.FileInfo Cor Ligthert Microsoft Dot NET 1 15th Sep 2004 08:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:05 PM.