C#中的FileNotFoundException
如果您要查找的文件不存在,则会发生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 …
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短