这必须是我见过的最奇怪的例外之一。使用File.WriteAllText静态方法写入文件时,无法写入特定的文件名:" PRN"。你可以用一个简单的衬垫重新创建它:
File.WriteAllText("c:\\Temp\\PRN.txt", "write this to a file");
我尝试了不同的扩展,我尝试使用此方法写入不同的文件夹,但在所有情况下,尝试使用此特定文件名编写文件时会出现以下异常:
FileStream不会打开Win32设备,如磁盘分区和 磁带机。避免使用\" \\。\\"在路上。
堆栈跟踪如下所示:
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 System.IO.File.WriteAllText(String path, String contents)
at WriteAllTextThrowsExceptionWithPRN.UnitTest1.TestMethod1() in c:\Users\ntregillus\Documents\Visual Studio 2013\Projects\WriteAllTextThrowsExceptionWithPRN\WriteAllTextThrowsExceptionWithPRN\UnitTest1.cs:line 14
为什么会出现这种情况?我想我会替换自己的文件编写器,但我发现文件名" PRD"会导致这个系统爆炸。
答案 0 :(得分:9)