C#实用文本记录存储器

这里记录一个C#文本记录导出小工具,代码很简单没什么逻辑,但是不经常写,容易忘记,这里记录一下,方便后续直接拿到手用,这里文件存储路径会在Unity中通过Log的形式输出,其他时候使用请替换Log输出方式。

using System.Collections.Generic;
using System.IO;
using UnityEngine;
/// 
/// 文本存储导出工具
/// 
public class TextWriteHelper 
{
    List list = new List();

    public void AddContent(string msg)
    {
        list.Add(msg);
    }
    public void SaveFile(string fileName)
    {
        string filePath = "";
        filePath = Application.persistentDataPath + "/TextContent/";
        if (!Directory.Exists(filePath))
        {
            Directory.CreateDirectory(filePath);
        }
        filePath = filePath + fileName + ".txt";
        FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter write = new StreamWriter(fs);
        for (int i = 0; i < list.Count; i++)
        {
            write.WriteLine(list[i]);
        }
        write.Flush();
        write.Close();
        write.Dispose();
        Debug.LogError($" file {fileName} save to {filePath}");
        list.Clear();
    }
}

你可能感兴趣的:(实用工具,c#,unity)