Cannot write to any path with filename "aux"

Issue

I’m perplexed to receive an exception when trying to write to a path that has the filename “aux”. Other filenames in the same and in different directories work perfectly and I never had this kind of trouble before.

To give you an idea, consider this test code:

Directory.CreateDirectory ("D:\\asdf"); // OK
File.WriteAllText ("D:\\asdf\\aux_", ""); // OK
try {
    File.WriteAllText ("D:\\asdf\\aux.txt", ""); // fails
}
catch (ArgumentException ex) {
    Console.WriteLine (ex);
}
try {
    File.WriteAllText ("D:\\asdf\\aux", ""); // fails
}
catch (ArgumentException ex) {
    Console.WriteLine (ex);
}
Console.WriteLine (string.Join ("\n", Directory.EnumerateFiles ("D:\\asdf")));

The final line prints that one file was created:

D:\asdf\aux_

Two equal exceptions are thrown:

System.ArgumentException: 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, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost)
   at Program.Main(String[] args) in C:\...\Program.cs:line 24

Replacing \\ with / did not change anything (and is nonstandard on Windows to my knowledge).

I’m aware aux had a special meaning a long time ago, but considering it only occurs in the filename (with or without an extension) in a subdirectory, it is not supposed to be related to that.

Solution

AUX is reserved name for a file. It is documented on Microsoft Docs at Naming Files, Paths, and Namespaces where they say:

Do not use the following reserved names for the name of a file: CON,
PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9,
LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid
these names followed immediately by an extension; for example, NUL.txt
is not recommended.

Answered By – Steve

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published