workaround for FileStream bug?

D

Dave Weeden

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)
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top