using System;
using System.IO;
class Test
{
public static void Main()
{
static void Main(string[] args)
{
string path = @"c:\MyTest.txt";
if (!File.Exists(path))
{
StreamWriter sw = File.CreateText(path);
sw.WriteLine("Hello");
sw.Close();
}
// Open the file to read from.
StreamReader sr = File.OpenText(path);
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
sr. Close ();
}
}
3. 编译执行,观察程序输出
问题:
1. 将path改成:path=@"c:\temp\MyTest.txt",而"c:\temp"文件夹不存在,观察程序运行结果。请修改代码,使程序能够正常处理异常。