当您尝试查找不存在的文件时,会引发找不到文件异常。
假设我在 StreamReader 中设置了一个不存在的文件“new.txt”。如果您尝试使用 StreamReader 访问它(读取它),它将抛出 FileNotFoundException -
using (StreamReader sReader = new StreamReader("new.txt")) { sReader.ReadToEnd(); }
要处理它,您需要使用 try 和 catch -
Try { using (StreamReader sReader = new StreamReader("new.txt")) { sReader.ReadToEnd(); } }catch (FileNotFoundException e) { Console.WriteLine("File Not Found!"); Console.WriteLine(e); }
以上就是如何捕获C#中的文件未找到异常?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号