.NET帮助文档上的一道题,大家帮忙看看
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt"; //我第一次运行竟然通过了,第二次把C改成D就抛异常,再改回C还是异常
// This text is added only once to the file.//为什么?还有那个@符号起什么作用啊,'\'和'/'有什么区别,是不是就不用
if (!File.Exists(path)) //转义了?
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path)) 这里抛出DirectoryNotFoundException异常
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}