Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2008

Tip: Looking for answers? Try searching our database.

workaround for FileStream bug?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave Weeden - 15 Jan 2008 22:50 GMT
Hi,

I've run across what I beleive to be a bug in the framework or the
underlying Win32 subsystem.  I've included the code that triggers the problem
as well as its diagnostic output.

Note that changing 'con.txt' to 'coon.txt' or something else rectifies the
problem so its something about that particular three letter combination.

Any ideas,

Dave

CODE:
-------

try
{
 using (FileStream stream = new FileStream("C:\\con.txt", FileMode.Create))
 {
   // do work
 }
}
catch (Exception e)
{
 System.Diagnostics.Debug.WriteLine(e.Message);
 System.Diagnostics.Debug.WriteLine(e.StackTrace);
}

OUTPUT:
----------

FileStream will not open Win32 devices such as disk partitions and tape
drives. Avoid use of "\\.\" in the path.
  at System.IO.FileStream.Init(String path, FileMode mode, FileAccess
access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean
bFromProxy)
  at System.IO.FileStream..ctor(String path, FileMode mode)
Misbah Arefin - 15 Jan 2008 23:12 GMT
CON is a reserved filename in any MS OS
CON = CONSOLE

Signature

Misbah Arefin

> Hi,
>
[quoted text clipped - 35 lines]
> bFromProxy)
>    at System.IO.FileStream..ctor(String path, FileMode mode)
Dave Weeden - 16 Jan 2008 01:50 GMT
Thanks, your comment pointed me the following link which lists a bunch of
reserved filenames including CON

http://msdn2.microsoft.com/en-us/library/aa365247.aspx

A better error message should be provided methinks.

> CON is a reserved filename in any MS OS
> CON = CONSOLE
[quoted text clipped - 38 lines]
> > bFromProxy)
> >    at System.IO.FileStream..ctor(String path, FileMode mode)
Walter Wang [MSFT] - 16 Jan 2008 06:48 GMT
Hi,

I agree this exception message is not very clear for users who are not very
familiar with Win32 devices.

By the way, this exception message does check such special device names,
it's just the error message only used one example "\\.\", and not all other
device names.

In this case, the .NET IO classes are internally using win32 API CreateFile
to create or open a file; and actually CreateFile is the same API used to
open these special device names. If you use following C code:

HANDLE hFile = CreateFile(_T("c:\\temp\\con.txt"), GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

You will find it succeeds by returning the handle to the stdout console.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.