gpt4 book ai didi

c# - 无法写入文件名为 "aux"的任何路径

转载 作者:可可西里 更新时间:2023-11-01 10:43:02 25 4
gpt4 key购买 nike

<分区>

在尝试写入文件名为“aux”的路径时收到异常,我感到很困惑。同一目录和不同目录中的其他文件名都可以正常工作,我以前从未遇到过这种麻烦。

为了给你一个想法,考虑这个测试代码:

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")));

最后一行打印出一个文件已创建:

D:\asdf\aux_

抛出两个相同的异常:

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

/ 替换 \\ 没有改变任何东西(据我所知,这在 Windows 上是非标准的)。

我知道 aux 很久以前有一个特殊的含义,但考虑到它只出现在子目录的文件名中(有或没有扩展名),不应该 与此相关。

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