using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace DaJiaoYan.Utils { internal class FileExts { /// /// 读取所有行 /// /// /// /// public static string[] ReadAllLines(string filename, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; string[] txt = { }; if (File.Exists(filename)) { txt = File.ReadAllLines(filename, encoding); } return txt; } /// /// 读取所有文本 /// /// /// /// public static string ReadAllText(string filename, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; string txt = string.Empty; if (File.Exists(filename)) { txt = File.ReadAllText(filename, encoding); } return txt; } /// /// 写入所有文本 /// /// /// /// public static void WriteAllText(string filename, string txt, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; CheckDirectory(Path.GetDirectoryName(filename), true); File.WriteAllText(filename, txt, encoding); } /// /// 写入二进制值 /// /// /// public static void WriteBytes(string filename, byte[] bytes) { if (bytes != null) { CheckDirectory(Path.GetDirectoryName(filename), true); File.WriteAllBytes(filename, bytes); } } /// /// 读取所有二进制 /// /// /// public static byte[] ReadBytes(string filename) { byte[] txt = default; if (File.Exists(filename)) { txt = File.ReadAllBytes(filename); } return txt; } /// /// 写入所有行 /// /// /// /// public static void WriteAllLines(string filename, string[] txt, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; CheckDirectory(Path.GetDirectoryName(filename), true); File.WriteAllLines(filename, txt, encoding); } /// /// 添加所有文本 /// /// /// /// public static void AppendAllText(string filename, string txt, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; CheckDirectory(Path.GetDirectoryName(filename), true); File.AppendAllText(filename, txt, encoding); } /// /// 检查目录是否存在,如果不存在,将根据条件创建或不创建 /// /// /// 如果目录不存在,是否创建 /// public static bool CheckDirectory(string p, bool create) { bool res = false; if (!string.IsNullOrEmpty(p)) { res = Directory.Exists(p); if (!res && create) { Directory.CreateDirectory(p); res = true; } } return res; } /// /// 在文件管理器中打开文件夹并选择文件 /// /// /// public static bool OpenFileInExplore(string filename, Action failCallback) { bool res = false; try { //System.Diagnostics.Process.Start("Explorer.exe", $"/select, {filename}"); ExplorerFile(filename, failCallback); res = true; } catch { } return res; } /// /// 打开路径并定位文件...对于@"h:\Bleacher Report - Hardaway with the safe call ??.mp4"这样的,explorer.exe /select,d:xxx不认,用API整它 /// /// 文件绝对路径 [DllImport("shell32.dll", ExactSpelling = true)] private static extern void ILFree(IntPtr pidlList); [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] private static extern IntPtr ILCreateFromPathW(string pszPath); [DllImport("shell32.dll", ExactSpelling = true)] private static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags); private static void ExplorerFile(string filePath, Action failCallback) { if (!File.Exists(filePath) && !Directory.Exists(filePath)) { failCallback?.Invoke(); return; } if (Directory.Exists(filePath)) Process.Start(@"explorer.exe", "/select,\"" + filePath + "\""); else { IntPtr pidlList = ILCreateFromPathW(filePath); if (pidlList != IntPtr.Zero) { try { Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0)); } finally { ILFree(pidlList); } } } } /// /// 获取文件夹下的图片,并按顺序排好 /// /// /// public static List GetImageFiles(string p) { List files = new List(), allFiles = new List(); try { allFiles = new List(Directory.GetFiles(p)); } catch { } files = allFiles.FindAll(x => { return Variables.Vars.AvalibleImageSuffixes.Exists(y => { return string.Equals(y, System.IO.Path.GetExtension(x), StringComparison.OrdinalIgnoreCase); }); }); files.Sort((a, b) => { if (a.Length == b.Length) { return a.CompareTo(b); } else { return a.Length.CompareTo(b.Length); } }); return files; } public static long GetHardDiskSpace(string str_HardDiskName) { long totalSize = 0; str_HardDiskName += ":\\"; System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo drive in drives) { if (string.Equals(drive.Name, str_HardDiskName, StringComparison.OrdinalIgnoreCase)) { totalSize = drive.TotalFreeSpace; } } return totalSize; } public static string GetHardDiskFreeSpace(string str_HardDiskFreeSpace, FileSizeUnit targetUnit = FileSizeUnit.MB) { long totalSize = GetHardDiskSpace(str_HardDiskFreeSpace); return Math.Floor(ToFileFormat(totalSize, targetUnit)).ToString() + Enum.GetName(typeof(FileSizeUnit), targetUnit); } /// /// 根据指定的文件大小单位,对输入的文件大小(字节表示)进行转换。 /// /// 文件文件大小,单位为字节。 /// 目标单位。 /// private static double ToFileFormat(long filesize, FileSizeUnit targetUnit = FileSizeUnit.MB) { double size; switch (targetUnit) { case FileSizeUnit.KB: size = filesize / 1024.0; break; case FileSizeUnit.MB: size = filesize / 1024.0 / 1024; break; case FileSizeUnit.GB: size = filesize / 1024.0 / 1024 / 1024; break; case FileSizeUnit.TB: size = filesize / 1024.0 / 1024 / 1024 / 1024; break; case FileSizeUnit.PB: size = filesize / 1024.0 / 1024 / 1024 / 1024 / 1024; break; default: size = filesize; break; } return size; } /// /// 文件大小单位,包括从B至PB共六个单位。 /// public enum FileSizeUnit { B, KB, MB, GB, TB, PB } } }