Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# File.WriteAllText真的抛出FileNotFoundException吗?_C#_File_Filenotfoundexception - Fatal编程技术网

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
throw
ArgumentException
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
throw
ArgumentException
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()文章。