如果您要查找的文件不存在,则会发生FileNotFoundException。
在这里,我们尝试使用StreamReader()method查找一个不存在的文件。
reader = new StreamReader("new.txt"))要阅读它,我们使用了以下方法-
reader.ReadToEnd();
让我们看完整的代码。
using System.IO;
using System;
class Program {
static void Main() {
using (StreamReader reader = new StreamReader("new.txt")) {
reader.ReadToEnd();
}
}
}由于文件“ new.txt”不存在,因此以上代码将生成以下异常。
Unhandled Exception: System.IO.FileNotFoundException: Could not find file …