workaround for FileStream bug?


Dave Weeden


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,



using (FileStream stream = new FileStream("C:\\con.txt", FileMode.Create))
// do work
catch (Exception e)


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
at System.IO.FileStream..ctor(String path, FileMode mode)

Walter Wang [MSFT]


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,

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

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