平时我们可以在操作系统的文件管理系统中,通过一些操作增删查改文件夹
我们目前要学习的就是通过代码的形式,来对文件夹进行增删查改的操作
- 类名:
Directory
- 命名空间:
using System.IO
一、Directory 类
(一)判断文件夹是否存在
if (Directory.Exists(Application.dataPath + "/数据持久化四")) {
print("存在文件夹");
}
else {
print("文件夹不存在");
}
(二)创建文件夹
DirectoryInfo info = Directory.CreateDirectory(Application.dataPath + "/数据持久化四");
(三)删除文件夹
// 参数一:路径
// 参数二:是否删除非空目录,如果为true,将删除整个目录,如果是false,仅当该目录为空时才可删除
Directory.Delete(Application.dataPath + "/数据持久化四");
(四)查找文件夹和文件
// 得到指定路径下所有文件夹名
string[] strs = Directory.GetDirectories(Application.dataPath);
for (int i = 0; i < strs.Length; i++) {
print(strs[i]);
}
// 得到指定路径下所有文件名
strs = Directory.GetFiles(Application.dataPath);
for (int i = 0; i < strs.Length; i++) {
print(strs[i]);
}
(五)移动文件夹
// 如果第二个参数所在的路径 已经存在了一个文件夹 那么会报错
// 移动会把文件夹中的所有内容一起移到新的路径
Directory.Move(Application.dataPath + "/数据持久化四", Application.dataPath + "/123123123");
二、DirectoryInfo 和 FileInfo
(一)创建文件夹方法的返回值
DirectoryInfo dInfo = Directory.CreateDirectory(Application.dataPath + "/数据持久化123");
// 全路径
print(dInfo.FullName);
// 文件名
print(dInfo.Name);
(二)查找上级文件夹信息
dInfo = Directory.GetParent(Application.dataPath + "/数据持久化123");
// 全路径
print(dInfo.FullName);
// 文件名
print(dInfo.Name);
// 重要方法
// 得到所有子文件夹的目录信息
DirectoryInfo[] dInfos = dInfo.GetDirectories();
// FileInfo文件信息类
// 我们可以通过DirectoryInfo得到该文件下的所有文件信息
FileInfo[] fInfos = dInfo.GetFiles();
for (int i = 0; i < fInfos.Length; i++) {
print(fInfos[i].Name); // 文件名
print(fInfos[i].FullName); // 路径
print(fInfos[i].Length); // 字节长度
print(fInfos[i].Extension); // 后缀名
}
Directory 提供给我们了常用的文件夹相关操作的 API,只需要熟练使用它即可
DirectoryInfo 和 FileInfo 一般在多文件夹和多文件操作时会用到,了解即可,目前用的相对较少。他们的用法和 Directory 和 File 类的用法大同小异