using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
|
namespace IStation.Untity
|
{
|
/// <summary>
|
/// 文件辅助类
|
/// </summary>
|
public class FileHelper
|
{
|
/// <summary>
|
///文件夹中不能包含的字符列表
|
/// </summary>
|
public static List<string> DisableStrList = new List<string> { "\\", "/", "*", "?", "\"", "<", ">", "|", ":" };
|
|
// 私有变量
|
private List<FileInfo> _lst = null;
|
|
/// <summary>
|
/// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
|
/// </summary>
|
/// <param name="path"></param>
|
/// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
|
/// <returns></returns>
|
public List<FileInfo> GetFile(string path, string extName)
|
{
|
_lst = new List<FileInfo>();
|
getdir2(path, extName);
|
return _lst;
|
}
|
|
/// <summary>
|
/// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
|
/// </summary>
|
/// <param name="path"></param>
|
/// <returns></returns>
|
public List<FileInfo> GetFile(string path)
|
{
|
_lst = new List<FileInfo>();
|
getdir1(path);
|
return _lst;
|
}
|
|
// 私有方法,递归获取指定类型文件,包含子文件夹
|
private void getdir2(string path, string extName)
|
{
|
try
|
{
|
string[] dir = Directory.GetDirectories(path); //文件夹列表
|
DirectoryInfo fdir = new DirectoryInfo(path);
|
FileInfo[] file = fdir.GetFiles();
|
//FileInfo[] file = Directory.GetFiles(path); //文件列表
|
if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
|
{
|
foreach (FileInfo f in file) //显示当前目录所有文件
|
{
|
if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
|
{
|
_lst.Add(f);
|
}
|
}
|
foreach (string d in dir)
|
{
|
getdir2(d, extName);//递归
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
|
// 私有方法,递归获取指定类型文件,包含子文件夹
|
private void getdir1(string path)
|
{
|
try
|
{
|
string[] dir = Directory.GetDirectories(path); //文件夹列表
|
DirectoryInfo fdir = new DirectoryInfo(path);
|
FileInfo[] file = fdir.GetFiles();
|
//FileInfo[] file = Directory.GetFiles(path); //文件列表
|
if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
|
{
|
foreach (FileInfo f in file) //显示当前目录所有文件
|
{
|
_lst.Add(f);
|
}
|
foreach (string d in dir)
|
{
|
getdir1(d);//递归
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
|
/// <summary>
|
/// 获取文件夹下的所有文件夹(包含子文件夹)
|
/// </summary>
|
public static List<DirectoryInfo> GetFolders(string path)
|
{
|
try
|
{
|
List<DirectoryInfo> list = new List<DirectoryInfo>();
|
var dir_root = new DirectoryInfo(path);
|
var dir_root_subs = dir_root.GetDirectories();
|
if (dir_root_subs != null && dir_root_subs.Length > 0)
|
{
|
foreach (var dir in dir_root_subs)
|
{
|
list.Add(dir);
|
var folders_sub = GetFolders(dir);
|
if (folders_sub != null && folders_sub.Count > 0)
|
list.AddRange(folders_sub);
|
}
|
}
|
return list;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
|
//递归获取
|
private static List<DirectoryInfo> GetFolders(DirectoryInfo directoryInfo)
|
{
|
List<DirectoryInfo> list = new List<DirectoryInfo>();
|
var dir_subs = directoryInfo.GetDirectories();
|
if (dir_subs != null && dir_subs.Length > 0)
|
{
|
foreach (var dir in dir_subs)
|
{
|
list.Add(dir);
|
var folders_sub = GetFolders(dir);
|
if (folders_sub != null && folders_sub.Count > 0)
|
list.AddRange(folders_sub);
|
}
|
}
|
return list;
|
}
|
|
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
|
/// <param name="FILE_NAME">文件路径</param>
|
/// <returns>文件的编码类型</returns>
|
public static System.Text.Encoding GetEncoding(string FILE_NAME)
|
{
|
System.IO.FileStream fs = new System.IO.FileStream(FILE_NAME, System.IO.FileMode.Open,
|
System.IO.FileAccess.Read);
|
System.Text.Encoding r = GetEncoding(fs);
|
fs.Close();
|
return r;
|
}
|
|
/// 通过给定的文件流,判断文件的编码类型
|
/// <param name="fs">文件流</param>
|
/// <returns>文件的编码类型</returns>
|
public static System.Text.Encoding GetEncoding(System.IO.FileStream fs)
|
{
|
byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
|
byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
|
byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
|
System.Text.Encoding reVal = System.Text.Encoding.Default;
|
|
System.IO.BinaryReader r = new System.IO.BinaryReader(fs, System.Text.Encoding.Default);
|
int i;
|
int.TryParse(fs.Length.ToString(), out i);
|
byte[] ss = r.ReadBytes(i);
|
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
|
{
|
reVal = System.Text.Encoding.UTF8;
|
}
|
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
|
{
|
reVal = System.Text.Encoding.BigEndianUnicode;
|
}
|
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
|
{
|
reVal = System.Text.Encoding.Unicode;
|
}
|
r.Close();
|
return reVal;
|
}
|
|
/// 判断是否是不带 BOM 的 UTF8 格式
|
/// <param name="data"></param>
|
/// <returns></returns>
|
private static bool IsUTF8Bytes(byte[] data)
|
{
|
int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
|
byte curByte; //当前分析的字节.
|
for (int i = 0; i < data.Length; i++)
|
{
|
curByte = data[i];
|
if (charByteCounter == 1)
|
{
|
if (curByte >= 0x80)
|
{
|
//判断当前
|
while (((curByte <<= 1) & 0x80) != 0)
|
{
|
charByteCounter++;
|
}
|
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
|
if (charByteCounter == 1 || charByteCounter > 6)
|
{
|
return false;
|
}
|
}
|
}
|
else
|
{
|
//若是UTF-8 此时第一位必须为1
|
if ((curByte & 0xC0) != 0x80)
|
{
|
return false;
|
}
|
charByteCounter--;
|
}
|
}
|
if (charByteCounter > 1)
|
{
|
throw new Exception("非预期的byte格式");
|
}
|
return true;
|
}
|
|
/// <summary>
|
/// 读取全部文本
|
/// </summary>
|
public static string ReadAllText(string fileName)
|
{
|
if (string.IsNullOrEmpty(fileName))
|
return default;
|
if (!File.Exists(fileName))
|
return default;
|
var encoding = GetEncoding(fileName);
|
return File.ReadAllText(fileName, encoding);
|
}
|
|
/// <summary>
|
/// 读取全部行
|
/// </summary>
|
public static List<string> ReadAllLines(string fileName)
|
{
|
if (string.IsNullOrEmpty(fileName))
|
return default;
|
if (!File.Exists(fileName))
|
return default;
|
var encoding = GetEncoding(fileName);
|
return File.ReadAllLines(fileName, encoding)?.ToList();
|
}
|
|
}
|
}
|