在上一篇中遇到了System.Text 命名空间,用于处理一些字符编码和字符串,这篇再记录一下项目中用到的文件操作的类
System.IO.Directory 文件夹操作类
System.IO.Directory.Exists(_path) 判断文件夹是否存在 通常配合System.IO.Directory.CreateDirectory(_path),
如不存在则创建文件夹。
Directory.GetFileSystemEntries(_path) 获取指定路径下的所有文件和文件夹名称,返回string[]类型
Directory.GetLogincalDrives() : 获取磁盘名称,返回string[]类型。
System.IO.File 文件操作类
System.IO.File.Exists(string path) 文件是否存在
System.IO.File.Delete(string path) 删除文件
System.IO.File.WriteAllBytes(string path, byte[] bytes) 将bytes[]写入路径为path的文件中
System.IO.File.WriteAllText(string path, string contents) 将string写入路径为path的文件中
那路径如何获取呢,我们可以用到Application类
常用路径是dataPath 和 presistentDataPath
dataPath 不同平台不一样,在UnityEditor中是项目所处的位置,在Asset文件夹下
eg:C:/Users/0001/Desktop/unityprojectName/Assets
presistentDataPath 是一个持久化数据存储目录,例如可以将项目下载的图片、声音文件存放在这里
eg:C:/Users/0001/AppData/LocalLow/XXXXX/unityprojectName
网友评论