using System;
using System.IO;
using System.Threading;class ThreadTest
{
private static Thread mytd;
public static void Main()
{
mytd = new Thread(new ThreadStart(CreatFile));
mytd.Start();
}
public static void CreatFile()
{
string path = @"C:\Threadtestaa\threadaa.txt";
if(!File.Exists(path))
{
Directory.CreateDirectory(@"C:\Threadtestaa");
File.CreateText(path);
}
int icount = 50;
while(icount<=50)
{
StreamWriter sw = new StreamWriter(path);
sw.WriteLine("你好"+DateTime.Now);
sw.Close();
System.Threading.Thread.Sleep(300);
++icount;
}
mytd.Abort();
}
}以上代码在运行的时候。出现IoException异常,异常说明为由另一个程序在使用C:\Threadtestaa\threadaa.txt这个文件。无法存取该文件。
想了很久不知道怎么回事。请高手指点一下。。非常感谢