1.利用File.CreateText()来创建文本文件。
代码如下:
代码如下:
static void Main(string[] args)
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
try
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("this is my name");
sw.WriteLine("what about you");
sw.Write("?I fine");
}
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
try
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("this is my name");
sw.WriteLine("what about you");
sw.Write("?I fine");
}
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}