string path =Server.MapPath("exercise.txt");
FileStream fs;
if( !File.Exists(path))
{
fs= new FileStream(path ,FileMode.Create,FileAccess.Write,FileShare.None);
}StreamWriter sw = new StreamWriter(path,false,Encoding.Default);
sw.Flush();
sw.Close();
fs=null;

解决方案 »

  1.   

    FileStream fs = null;
    try
    {
    fs = new FileStream(fullFileName,FileMode.Create,FileAccess.Write);
    StreamWriter sw = new StreamWriter(fs,Encoding.Default);
    sw.Write(fileContent);
    sw.Close();
    }
    finally
    {
    if(fs != null)
    fs.Close();
    }
      

  2.   

    FileStream FS=new FileStream (@"C:\d.txt", FileMode.OpenOrCreate, FileAccess.Write);
    using (StreamWriter sw = new StreamWriter(FS)) 
    {
    sw.Write("This is the ");
    }
      

  3.   

    string path = Server.MapPath("exercise.txt");
    Response.Write(path+"<br>");
    if( !File.Exists(path))
    {
    File.Create(path);

    }
    StreamWriter sw = File.OpenWrite();  //注意此行.
    sw.Write("yes");
    sw.Close();
      

  4.   

    在finally里面写:
    {
    if(fs != null)
    fs.Close();
    }