string str="function showtop(){}"+"\nfunction showbot(){}";如果指定的TXT不存在则创建,如果存在则先删除再创建,创建好之后把字符串写入到TXT文件中去啊?我刚学C#,不知道怎么做,请给我完整的代码好吗?谢谢各位大大了!

解决方案 »

  1.   

    StreamWriter SW;
    string filenmae=Application.StartupPath.ToString()+@"\log.txt";
    SW=File.AppendText(filenmae);
    SW.WriteLine(System.DateTime.Now.ToString("yyyyMMdd HH:mm:ss")+"    "+str);
    SW.Close();
      

  2.   

    string str="function showtop(){}"+"\nfunction showbot(){}";
    string path = @"D:\MyTest.txt";
    if (!File.Exists(path)) 
    {
    // Create a file to write to.
    using (StreamWriter sw = File.CreateText(path)) 
    {
    sw.Write(str);
    }
    }
    else
    {
    File.Delete(path);
    using (StreamWriter sw =new StreamWriter(path))  
    {
    sw.Write(str);
    }
    }要确保有权限