C# File.WriteAllText真的抛出FileNotFoundException吗?
文件说:C# File.WriteAllText真的抛出FileNotFoundException吗?,c#,file,filenotfoundexception,C#,File,Filenotfoundexception,文件说: // Summary: // Creates a new file, writes the specified string to the file, and then closes // the file. If the target file already exists, it is overwritten. 第一行第一句:创建一个新文件,并在其列出的异常上: // System.IO.FileNotFoundException: // The fi
// Summary:
// Creates a new file, writes the specified string to the file, and then closes
// the file. If the target file already exists, it is overwritten.
第一行第一句:创建一个新文件
,并在其列出的异常上:
// System.IO.FileNotFoundException:
// The file specified in path was not found.
在哪种情况下会发生这种情况?如果它总是创建一个文件,那么它不应该抛出FileNotFoundException
文档是否有误?或者它可能缺少一个
标记?文件。writealText
最终调用:
private static void InternalWriteAllText(string path, string contents, Encoding encoding)
{
using (StreamWriter streamWriter = new StreamWriter(path, false, encoding))
{
streamWriter.Write(contents);
}
}
调用InternalWriteAllText
throwArgumentException
或ArgumentNullException
之前引发的所有异常,但理论上(因为FileStream
可以引发异常)streamWriter.Write(内容)代码>可能引发异常。根据它的功能和如何打开streamWriter
,可能性非常小
我不一定要说文件本身是错误的,更多的是MS通过记录(非常罕见的)可能性来掩盖他们的屁股
来源:使用ILSpy反编译mscorlib
v4.0.0.0
更新
刚刚选中了mscorlib
v2.0.0.0,除了包含较少的健全性检查(这意味着它基本上直接转换为上面的代码)之外,情况是相同的 File.WriteAllText
最终调用:
private static void InternalWriteAllText(string path, string contents, Encoding encoding)
{
using (StreamWriter streamWriter = new StreamWriter(path, false, encoding))
{
streamWriter.Write(contents);
}
}
调用InternalWriteAllText
throwArgumentException
或ArgumentNullException
之前引发的所有异常,但理论上(因为FileStream
可以引发异常)streamWriter.Write(内容)代码>可能引发异常。根据它的功能和如何打开streamWriter
,可能性非常小
我不一定要说文件本身是错误的,更多的是MS通过记录(非常罕见的)可能性来掩盖他们的屁股
来源:使用ILSpy反编译mscorlib
v4.0.0.0
更新
刚刚选中了mscorlib
v2.0.0.0,除了包含较少的健全性检查(这意味着它基本上直接转换为上面的代码)之外,情况是相同的 如果找不到路径的一部分该怎么办。这是FileNotFoundException
还是DirectoryNotFoundException
?@johnsaunds这是DirectoryNotFoundException
:{“找不到路径'C:\\zzzzzzz\\ZZZ\\TEST.txt'的一部分”。}
这是文件中的复制粘贴错误。ReadAllText()文章。如果找不到路径的一部分该怎么办。这是FileNotFoundException
还是DirectoryNotFoundException
?@johnsaunds是DirectoryNotFoundException
:{“找不到路径“C:\\zzzzzzzz\\ZZZ\\TEST.txt”的一部分。”}
这是文件中的复制粘贴错误。ReadAllText()文章。