假设我在 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#中的文件未找到异常?的详细内容。