FileExtensions
FileExtensions 功能文档
Section titled “FileExtensions 功能文档”FileExtensions 是一个静态扩展类,为文件系统相关类型(string、FileInfo、DirectoryInfo)提供了丰富的扩展方法,涵盖文件读写、路径处理、信息获取、属性操作等功能,旨在简化文件系统操作,提高代码的安全性和可读性,特别适用于各种文件处理场景。
主要功能模块
Section titled “主要功能模块”1. 文件判断
Section titled “1. 文件判断”Exists
Section titled “Exists”判断文件是否存在。
public static bool Exists(this string filePath)示例:
var exists = "C:\\test.txt".Exists(); // falseIsFile
Section titled “IsFile”判断路径是否为文件。
public static bool IsFile(this string path)IsDirectory
Section titled “IsDirectory”判断路径是否为目录。
public static bool IsDirectory(this string path)ExistsPath
Section titled “ExistsPath”判断路径的文件或目录是否存在。
public static bool ExistsPath(this string path)IsReadOnly
Section titled “IsReadOnly”判断文件是否为只读。
public static bool IsReadOnly(this string filePath)IsHidden
Section titled “IsHidden”判断文件是否为隐藏文件。
public static bool IsHidden(this string filePath)IsSystemFile
Section titled “IsSystemFile”判断文件是否为系统文件。
public static bool IsSystemFile(this string filePath)IsTemporary
Section titled “IsTemporary”判断文件是否为临时文件。
public static bool IsTemporary(this string filePath)2. 同步读取
Section titled “2. 同步读取”ReadAllText
Section titled “ReadAllText”读取文件所有文本内容。
public static string ReadAllText(this string filePath)public static string ReadAllText(this string filePath, Encoding encoding)示例:
var content = "C:\\test.txt".ReadAllText();var contentUtf8 = "C:\\test.txt".ReadAllText(Encoding.UTF8);ReadAllLines
Section titled “ReadAllLines”读取文件所有行内容。
public static string[] ReadAllLines(this string filePath)public static string[] ReadAllLines(this string filePath, Encoding encoding)ReadAllBytes
Section titled “ReadAllBytes”读取文件所有字节内容。
public static byte[] ReadAllBytes(this string filePath)ReadLines
Section titled “ReadLines”逐行读取文件内容(延迟执行)。
public static IEnumerable<string> ReadLines(this string filePath)public static IEnumerable<string> ReadLines(this string filePath, Encoding encoding)3. 异步读取
Section titled “3. 异步读取”ReadAllTextAsync
Section titled “ReadAllTextAsync”异步读取文件所有文本内容。
public static async Task<string> ReadAllTextAsync(this string filePath, CancellationToken cancellationToken = default)public static async Task<string> ReadAllTextAsync(this string filePath, Encoding encoding, CancellationToken cancellationToken = default)示例:
var content = await "C:\\test.txt".ReadAllTextAsync();ReadAllLinesAsync
Section titled “ReadAllLinesAsync”异步读取文件所有行内容。
public static async Task<List<string>> ReadAllLinesAsync(this string filePath, CancellationToken cancellationToken = default)public static async Task<List<string>> ReadAllLinesAsync(this string filePath, Encoding encoding, CancellationToken cancellationToken = default)ReadAllBytesAsync
Section titled “ReadAllBytesAsync”异步读取文件所有字节内容。
public static async Task<byte[]> ReadAllBytesAsync(this string filePath, CancellationToken cancellationToken = default)4. 同步写入
Section titled “4. 同步写入”WriteAllText
Section titled “WriteAllText”写入文本内容到文件(覆盖)。
public static void WriteAllText(this string filePath, string content)public static void WriteAllText(this string filePath, string content, Encoding encoding)示例:
"C:\\test.txt".WriteAllText("Hello World");WriteAllLines
Section titled “WriteAllLines”写入多行内容到文件(覆盖)。
public static void WriteAllLines(this string filePath, IEnumerable<string> lines)public static void WriteAllLines(this string filePath, IEnumerable<string> lines, Encoding encoding)WriteAllBytes
Section titled “WriteAllBytes”写入字节内容到文件(覆盖)。
public static void WriteAllBytes(this string filePath, byte[] bytes)AppendText
Section titled “AppendText”追加文本内容到文件。
public static void AppendText(this string filePath, string content)public static void AppendText(this string filePath, string content, Encoding encoding)AppendLines
Section titled “AppendLines”追加多行内容到文件。
public static void AppendLines(this string filePath, IEnumerable<string> lines)public static void AppendLines(this string filePath, IEnumerable<string> lines, Encoding encoding)AppendLine
Section titled “AppendLine”追加一行内容到文件。
public static void AppendLine(this string filePath, string line)5. 异步写入
Section titled “5. 异步写入”WriteAllTextAsync
Section titled “WriteAllTextAsync”异步写入文本内容到文件(覆盖)。
public static async Task WriteAllTextAsync(this string filePath, string content, CancellationToken cancellationToken = default)public static async Task WriteAllTextAsync(this string filePath, string content, Encoding encoding, CancellationToken cancellationToken = default)示例:
await "C:\\test.txt".WriteAllTextAsync("Hello World");WriteAllLinesAsync
Section titled “WriteAllLinesAsync”异步写入多行内容到文件(覆盖)。
public static async Task WriteAllLinesAsync(this string filePath, IEnumerable<string> lines, CancellationToken cancellationToken = default)public static async Task WriteAllLinesAsync(this string filePath, IEnumerable<string> lines, Encoding encoding, CancellationToken cancellationToken = default)WriteAllBytesAsync
Section titled “WriteAllBytesAsync”异步写入字节内容到文件(覆盖)。
public static async Task WriteAllBytesAsync(this string filePath, byte[] bytes, CancellationToken cancellationToken = default)AppendTextAsync
Section titled “AppendTextAsync”异步追加文本内容到文件。
public static async Task AppendTextAsync(this string filePath, string content, CancellationToken cancellationToken = default)AppendLinesAsync
Section titled “AppendLinesAsync”异步追加多行内容到文件。
public static async Task AppendLinesAsync(this string filePath, IEnumerable<string> lines, CancellationToken cancellationToken = default)6. 文件操作
Section titled “6. 文件操作”DeleteFile
Section titled “DeleteFile”删除文件。
public static void DeleteFile(this string filePath)示例:
"C:\\test.txt".DeleteFile();CopyTo
Section titled “CopyTo”复制文件到目标路径。
public static void CopyTo(this string sourcePath, string destPath, bool overwrite = true)MoveTo
Section titled “MoveTo”移动文件到目标路径。
public static void MoveTo(this string sourcePath, string destPath, bool overwrite = true)Rename
Section titled “Rename”重命名文件。
public static void Rename(this string filePath, string newName)Replace
Section titled “Replace”替换文件内容。
public static void Replace(this string sourcePath, string destPath, string backupPath = null)7. 文件信息
Section titled “7. 文件信息”GetFileSize
Section titled “GetFileSize”获取文件大小(字节)。
public static long GetFileSize(this string filePath)示例:
var size = "C:\\test.txt".GetFileSize();GetCreationTime
Section titled “GetCreationTime”获取文件创建时间。
public static DateTime GetCreationTime(this string filePath)GetLastWriteTime
Section titled “GetLastWriteTime”获取文件最后修改时间。
public static DateTime GetLastWriteTime(this string filePath)GetLastAccessTime
Section titled “GetLastAccessTime”获取文件最后访问时间。
public static DateTime GetLastAccessTime(this string filePath)GetFileSizeFriendly
Section titled “GetFileSizeFriendly”获取文件大小的友好字符串(如 “1.23 MB”)。
public static string GetFileSizeFriendly(this string filePath)8. 文件属性
Section titled “8. 文件属性”SetReadOnly
Section titled “SetReadOnly”设置文件为只读。
public static void SetReadOnly(this string filePath)UnsetReadOnly
Section titled “UnsetReadOnly”取消文件只读属性。
public static void UnsetReadOnly(this string filePath)SetHidden
Section titled “SetHidden”设置文件为隐藏文件。
public static void SetHidden(this string filePath)UnsetHidden
Section titled “UnsetHidden”取消文件隐藏属性。
public static void UnsetHidden(this string filePath)SetAttributes
Section titled “SetAttributes”设置文件属性。
public static void SetAttributes(this string filePath, FileAttributes attributes)GetAttributes
Section titled “GetAttributes”获取文件属性。
public static FileAttributes GetAttributes(this string filePath)9. 哈希计算
Section titled “9. 哈希计算”GetFileMd5
Section titled “GetFileMd5”获取文件的 MD5 值。
public static string GetFileMd5(this string filePath)示例:
var md5 = "C:\\test.txt".GetFileMd5();GetFileSha256
Section titled “GetFileSha256”获取文件的 SHA256 值。
public static string GetFileSha256(this string filePath)GetFileHash
Section titled “GetFileHash”获取文件的指定算法哈希值。
public static string GetFileHash(this string filePath, HashAlgorithm algorithm)10. 路径处理
Section titled “10. 路径处理”GetExtension
Section titled “GetExtension”获取文件扩展名(带点)。
public static string GetExtension(this string filePath)GetFileName
Section titled “GetFileName”获取文件名(带路径)。
public static string GetFileName(this string filePath)GetFileNameWithoutExtension
Section titled “GetFileNameWithoutExtension”获取文件名(不带扩展名)。
public static string GetFileNameWithoutExtension(this string filePath)GetDirectoryName
Section titled “GetDirectoryName”获取文件所在目录路径。
public static string GetDirectoryName(this string filePath)CombinePaths
Section titled “CombinePaths”合并多个路径为一个完整路径。
public static string CombinePaths(this string path, params string[] paths)示例:
var fullPath = "C:\\Base".CombinePaths("SubDir", "file.txt");// "C:\\Base\\SubDir\\file.txt"GetFullPath
Section titled “GetFullPath”获取路径的完整规范路径(相对路径转绝对路径)。
public static string GetFullPath(this string relativePath)GetPathRoot
Section titled “GetPathRoot”获取路径的根目录。
public static string GetPathRoot(this string path)GetParentDirectory
Section titled “GetParentDirectory”获取路径的父目录路径。
public static string GetParentDirectory(this string path)ChangeExtension
Section titled “ChangeExtension”更改路径的扩展名。
public static string ChangeExtension(this string path, string newExtension)NormalizePath
Section titled “NormalizePath”获取路径的规范化形式。
public static string NormalizePath(this string path)GetRelativePath
Section titled “GetRelativePath”获取路径的相对路径。
public static string GetRelativePath(this string path, string basePath)11. 路径判断
Section titled “11. 路径判断”IsAbsolute
Section titled “IsAbsolute”判断路径是否为绝对路径。
public static bool IsAbsolute(this string path)IsRelative
Section titled “IsRelative”判断路径是否为相对路径。
public static bool IsRelative(this string path)IsUncPath
Section titled “IsUncPath”判断路径是否为 UNC 路径。
public static bool IsUncPath(this string path)HasInvalidChars
Section titled “HasInvalidChars”判断路径是否包含无效字符。
public static bool HasInvalidChars(this string path)IsRootDirectory
Section titled “IsRootDirectory”判断路径是否为根目录。
public static bool IsRootDirectory(this string path)12. 目录操作
Section titled “12. 目录操作”CreateDirectory
Section titled “CreateDirectory”创建目录。
public static void CreateDirectory(this string directoryPath)DeleteDirectory
Section titled “DeleteDirectory”删除目录。
public static void DeleteDirectory(this string directoryPath, bool recursive = true)GetFiles
Section titled “GetFiles”获取目录下的所有文件。
public static string[] GetFiles(this string directoryPath, string searchPattern = "*.*", bool recursive = false)GetDirectories
Section titled “GetDirectories”获取目录下的所有子目录。
public static string[] GetDirectories(this string directoryPath, string searchPattern = "*", bool recursive = false)GetTempFilePath
Section titled “GetTempFilePath”获取临时文件路径。
public static string GetTempFilePath()GetTempDirectory
Section titled “GetTempDirectory”获取临时目录路径。
public static string GetTempDirectory()- 文件管理 - 读写文件、复制移动文件、获取文件信息等操作
- 路径处理 - 路径拼接、分解、规范化等
- 文件系统遍历 - 创建删除、移动目录和文件
- 文件验证 - 检查文件是否存在、验证路径有效性
- 文件属性管理 - 设置文件只读、隐藏等属性
- 文件校验 - 计算文件哈希值进行完整性校验
- 目录遍历 - 获取目录下的文件和子目录信息
- 临时文件管理 - 创建和管理临时文件及目录
- 所有方法都是扩展方法,需要通过相应实例来调用
- 带有 “Safe” 后缀的方法对 null 值进行了安全处理,不会抛出异常
- 大部分文件操作方法会先检查文件是否存在,避免操作不存在的文件
- 路径处理方法支持跨平台路径分隔符处理
- 文件哈希计算方法使用 using 确保资源正确释放
- 递归方法提供可选参数控制递归深度
- 属性操作方法在操作前会检查文件是否存在
- 所有读写方法对 null 参数进行了安全处理
- 路径规范化方法使用 Uri 进行跨平台兼容处理