写入txt文件,最后需要关闭写入流。

    1. void OutputString()
    2. {
    3. test_path="TxtFiles/test.txt";
    4. StreamWriter sw = new StreamWriter(test_path);
    5. sw.WriteLine("输出时间:" + DateTime.Now.ToString());
    6. sw.WriteLine("输出内容:需要放到txt中的内容。");
    7. sw.Close();
    8. List<string> NameList=new List<string>();
    9. StreamWriter sw = new StreamWriter(test_path);
    10. foreach (string Name in NameList)
    11. {
    12. sw.WriteLine("输出时间:" + DateTime.Now.ToString());
    13. sw.WriteLine("输出内容:"+ Name);
    14. }
    15. sw.Close();
    16. }