File.WriteAllText是否真的抛出FileNotFoundException?

Bru*_*oLM 1 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.
Run Code Online (Sandbox Code Playgroud)

第一行,第一句:Creates a new file和它列出的例外情况:

//   System.IO.FileNotFoundException:
//     The file specified in path was not found.
Run Code Online (Sandbox Code Playgroud)

在哪种情况下会发生这种情况?如果它总是创建一个文件,那么它不应该抛出FileNotFoundException ...

文档错了吗?或者是否遗漏了一个<remarks>标签?

M.B*_*ock 5

File.WriteAllText 最终要求:

private static void InternalWriteAllText(string path, string contents, Encoding encoding)
{
    using (StreamWriter streamWriter = new StreamWriter(path, false, encoding))
    {
        streamWriter.Write(contents);
    }
}
Run Code Online (Sandbox Code Playgroud)

在调用InternalWriteAllTextthrow 之前抛出的所有异常ArgumentException或者ArgumentNullException理论上(因为FileStream可以抛出异常)streamWriter.Write(contents);可能会抛出异常.虽然根据它的作用以及如何streamWriter打开它,但不太可能.

我不一定说文档本身是错误的,更多的是MS通过记录(非常罕见的)可能性来覆盖他们的屁股.

来源:mscorlib使用ILSpy 反编译v4.0.0.0.

UPDATE

刚检查了mscorlibv2.0.0.0,相同的情况除了包含较少的健全性检查(意味着它基本上直接转换为上面的代码).