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.