文档中写道:
// 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.第一行,第一句话:Creates a new file,以及它列出的异常:
// System.IO.FileNotFoundException:
// The file specified in path was not found.在什么情况下会发生这种情况?如果它总是创建一个文件,那么它就不应该抛出FileNotFoundException...
文档有误吗?或者它可能缺少一个<remarks>标签?
发布于 2012-02-18 12:34:49
File.WriteAllText最终调用:
private static void InternalWriteAllText(string path, string contents, Encoding encoding)
{
using (StreamWriter streamWriter = new StreamWriter(path, false, encoding))
{
streamWriter.Write(contents);
}
}在调用InternalWriteAllText之前抛出的所有异常都会抛出ArgumentException或ArgumentNullException,但理论上(因为FileStream可以抛出异常),streamWriter.Write(contents);可能会抛出异常。不过,从它的作用和streamWriter的打开方式来看,这是不太可能的。
我不一定要说医生本身是错的,更多的是MS通过记录(非常罕见的)可能性来掩盖他们的臀部。
来源:使用ILSpy反编译mscorlib v4.0.0.0。
更新
刚刚检查了mscorlib v2.0.0.0,情况相同,只是它包含的健全性检查较少(这意味着它基本上直接转换为上面的代码)。
https://stackoverflow.com/questions/9338512
复制相似问题