using System.IO;
using System.Linq;
public static class FileUtils
{
///
/// 遍历文件夹中的文件【仅仅遍历当前目录】
///
///
///
///
///
public static string[] TraverseFiles(string dir, string filter, string option = "")
{
if (string.IsNullOrEmpty(option))
return Directory.GetFiles(dir, filter, SearchOption.TopDirectoryOnly);
else
return Directory.GetFiles(dir, filter, SearchOption.TopDirectoryOnly).Where(s => option.Contains(Path.GetExtension(s).ToLower())).ToArray();
}
///
/// 遍历文件夹中的文件
///
/// 文件夹
/// 查找的文件后缀
///
///
public static string[] TraverseAllFiles(string dir, string filter, string option = "")
{
if (string.IsNullOrEmpty(option))
return Directory.GetFiles(dir, filter, SearchOption.AllDirectories);
else
return Directory.GetFiles(dir, filter, SearchOption.AllDirectories).Where(s => option.Contains(Path.GetExtension(s).ToLower())).ToArray();
}
///
/// 获取资源相对路径
///
/// 资源路径
/// 在Editor下的资源相对路径
public static string ExtractAssetRelativePath(string filePath)
{
filePath = filePath.Replace('\\', '/');
int pos = filePath.IndexOf("Assets");
if (pos == -1)
return string.Empty;
string relativePath = filePath.Substring(pos, filePath.Length - pos);
return relativePath;
}
///
/// 去掉文件后缀
///
/// 文件名字
///
public static string RemoveExtension(string fileName)
{
int pos = fileName.LastIndexOf(".");
if (pos == -1)
return fileName;
return fileName.Substring(0, pos);
}
///
/// 文件是否有后缀
///
///
///
public static bool HasExtension(string fileName)
{
int pos = fileName.LastIndexOf(".");
if (pos == -1)
return false;
return true;
}
///
/// 获取文件名字,去掉文件的目录
///
///
///
public static string ExtractPureName(string filePath)
{
filePath = filePath.Replace('\\', '/');
int pos = filePath.LastIndexOf("/");
if (pos == -1)
return filePath;
return filePath.Substring(pos + 1, filePath.Length - pos - 1);
}
///
/// 获取文件所在目录
///
///
///
public static bool ExtractParent(string filePath,out string dir)
{
dir = null;
filePath = filePath.Replace('\\', '/');
int pos = filePath.LastIndexOf("/");
if (pos == -1)
return false;
dir = filePath.Substring(0, pos);
return true;
}
public static string RemoveParent(string parent,string filePath)
{
filePath = filePath.Replace('\\', '/');
int pos = filePath.IndexOf(parent);
if (pos == -1)
return filePath;
string relativePath = filePath.Substring(parent.Length+1);
return relativePath;
}
}