gpt4 book ai didi

c# - System.IO.File.WriteAllText 为特定文件名抛出异常

转载 作者:太空狗 更新时间:2023-10-29 22:30:53 28 4
gpt4 key购买 nike

这一定是我见过的最奇怪的异常之一。使用File.WriteAllText静态方法写入文件时,不能写入特定文件名:“PRN”。你可以用一个简单的衬垫重新创建它:

File.WriteAllText("c:\\Temp\\PRN.txt", "write this to a file");

我尝试了不同的扩展,我尝试用这种方法写入不同的文件夹,但在所有情况下,当我尝试写入具有这个特定文件名的文件时,我都会遇到以下异常:

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 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”会导致这个系统崩溃,这很疯狂。

最佳答案

这不是因为您的代码。 PRN 是 MSDOS 设备名称,不能用作文件名。

使用不同的文件名。

https://support.microsoft.com/en-us/kb/74496

关于c# - System.IO.File.WriteAllText 为特定文件名抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27306821/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com