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 …
热门推荐
8 天仓节祝福语简短
10 六一祝福语简短网名
11 晋城春节祝福语简短英文
12 霸气新年祝福语大全简短
13 年会公司新年祝福语简短
14 新春祝福语大全 英语简短
15 伴郎红包祝福语朋友简短
16 新年儿童祝福语大全 简短
17 女孩自己生日祝福语简短
18 婆婆66岁祝福语简短