现在的位置: 首页 > 综合 > 正文

Unity3D CreateFile

2013年10月29日 ⁄ 综合 ⁄ 共 909字 ⁄ 字号 评论关闭

using UnityEngine;
using System.Collections;
using System.IO;

public class Script_08_02 : MonoBehaviour
{
    void Start ()
    {
        Debug.Log(Application.dataPath);
        CreateFile(Application.dataPath, "FileName","TestInfo0");
        CreateFile(Application.dataPath, "FileName", "TestInfo1");
        CreateFile(Application.dataPath, "FileName", "TestInfo2");
    }

    /// <summary>
    /// 创建一个文件
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="name">名称</param>
    /// <param name="info">写入的内容</param>
    void CreateFile(string path, string name, string info)
    {
        // 文件流信息
        StreamWriter sw;
        FileInfo t = new FileInfo(path + "//" + name);

        if (!t.Exists)
        {
            // 如果文件不存在则创建
            sw = t.CreateText();
        }
        else
        {
            // 如果文件存在,则打开该文件
            sw = t.AppendText();
        }

        // 以行的形式写入信息
        sw.WriteLine(info);

        // 关闭流
        sw.Close();

        // 销毁流
        sw.Dispose();

    }
}

抱歉!评论已关闭.