Unity--System.IO文件操作

来源:互联网 发布:bbs.h5dm新域名 编辑:程序博客网 时间:2024/05/17 00:52

今天看到System.IO的一些教程,是对windows下文件或文件夹的增删改的操作。

已经写好了注释,按照注释一步步看就可以了。

using UnityEngine;using System.Collections;using System.IO; public class Test : MonoBehaviour {     void Start()    {        CreateFileAndDirector();         WriteTextFile();         ReadTextFile();         CopyAndMoveFile();    }     /// <summary>    /// 创建文件夹“MyTest”和文件“1.txt”    /// </summary>    void CreateFileAndDirector()    {        // 指定一个“当前活动文件夹”        string activeDir = @"d:\";         // 合并路径字符串 组合成:d:\MyTest        string newPath = Path.Combine(activeDir, "MyTest");         // 创建文件夹        Directory.CreateDirectory(newPath);         // 创建一个新文件的名字        string newFileName = "1.txt";         // 指定该文件位置        newPath = Path.Combine(newPath, newFileName);         // 创建文件        if (!File.Exists(newPath))        {            File.Create(newPath);        }     }     /// <summary>    /// 写入文件    /// </summary>    void WriteTextFile()    {        string path = @"d:\MyTest\1.txt";         string text = "A class is the most powerful data type in C#. Like structures, " +                           "a class defines the data and behavior of the data type. ";        // 不分行        File.WriteAllText(path, text);         string[] lines = { "First line", "Second line", "Third line" };        // 写到三行        File.WriteAllLines(@"d:\MyTest\1.txt", lines);    }     /// <summary>    /// 读取文件    /// </summary>    void ReadTextFile()    {        // 读取所有内容        string text = File.ReadAllText(@"d:\MyTest\1.txt");        Debug.Log(text);         // 分行读取所有内容        string[] lines = File.ReadAllLines(@"d:\MyTest\1.txt");        Debug.Log(lines[0]);    }     /// <summary>    /// 复制和移动文件    /// </summary>    void CopyAndMoveFile()    {        string fileName = "1.txt";         string sourcePath = @"d:\MyTest";        string targetPath = @"d:\MyTest\SubDir";          string sourceFile = Path.Combine(sourcePath, fileName);        string destFile = Path.Combine(targetPath, fileName);         if (!Directory.Exists(targetPath))        {            Directory.CreateDirectory(targetPath);        }         // 复制文件,TRUE为如果目标目录已存在该文件,则覆盖;FALSE已存在该文件 则取消复制        File.Copy(sourceFile, destFile, true);         // 移动文件        //File.Move(sourceFile, destFile);    }}
转载自:网虫虫u3d8.com 发表过

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 醉蟹生吃还是熟吃 无锡熟醉蟹做法 醉蟹怎么做 无锡醉蟹 醉蟹怎么吃 正宗上海醉蟹详细做法 醉蟹有寄生虫吗 醉蟹的制作方法 大纵湖醉蟹价格 上海汪姐醉蟹制作方法 熟醉蟹的做法和调料 醉蟹多少钱一斤 醉梭子蟹的制作方法 正宗熟醉蟹的做法窍门 上海熟醉蟹的做法大全 醉蟹的做法和调料窍门 正宗醉蟹的做法大全 醉蟹吃法图解 醉蟹钳的做法 上海醉蟹的做法 醉蟹的做法大全 醉蟹图片 醉酒处罚 醉酒的蝴蝶 初恋醉酒吻 醉酒的诗句 刘龄醉酒 刘仱醉酒 醉酒后的插入 醉酒蝴蝶原唱 醉酒的蝴蝶原唱 酒精中毒的表现 急性酒精中毒症状 醉驾标准 醉驾的处罚 醉驾判刑多久 醉驾也能从宽免刑 2018醉驾判刑多久 醉驾2019年新规放宽 2018醉驾标准放宽 2018新交规醉驾怎么判