Class FileUtil
Utility to manage files.
public static class FileUtil
- Inheritance
-
FileUtil
- Inherited Members
Methods
CopyDirectory(string, string)
Copies a directory and all its contents to a destination folder.
public static void CopyDirectory(string sourceFolder, string destFolder)
Parameters
CreateWithDirectoryAndGetFileStream(string)
Creates or loads a file with directory.
public static FileStream CreateWithDirectoryAndGetFileStream(string filePath)
Parameters
filePathstringfile path
Returns
- FileStream
file stream
CreateWithDirectoryIfNotExisted(string)
public static bool CreateWithDirectoryIfNotExisted(string filePath)
Parameters
filePathstring
Returns
- bool
true if file created.
DetectRoundTripEncoding(string)
Detects a text encoding for filePath that survives a
read-modify-write round trip byte for byte on untouched lines.
public static Encoding DetectRoundTripEncoding(string filePath)
Parameters
filePathstringThe path of the file to sniff.
Returns
- Encoding
The encoding of the UTF-8 / UTF-16 byte-order mark when one is present; otherwise strict-validated UTF-8 (without a mark); otherwise Latin-1.
Remarks
The Latin-1 fallback maps every byte to the char of the same value, so a file in any ANSI-family encoding (GBK, Big5, Shift-JIS, ...) re-encodes to its original bytes. ASCII-keyed parsing is unaffected: those encodings never reuse bytes below 0x40 as trail bytes, so delimiters such as ‘;’ and ‘(’ cannot occur inside a multi-byte character. The BCL's default UTF-8 reader instead replaces every undecodable byte with U+FFFD, which irreversibly destroys such content on write-back.
EnsureDirectory(string)
Ensures that the specified directory exists, creating it if necessary.
public static void EnsureDirectory(string directory)
Parameters
directorystringThe directory path to ensure exists.
GetAbsentPath(string, string)
Gets a path that doesn't exist by appending a number to the base path.
public static string GetAbsentPath(string pathWithoutExtension, string extension)
Parameters
Returns
- string
A path that doesn't exist.
GetAbsentRelPath(string, string, string)
Gets a relative path that doesn't exist by appending a number to the base path.
public static string GetAbsentRelPath(string baseDirectory, string relPathWithoutExtension, string extension)
Parameters
baseDirectorystringThe base directory.
relPathWithoutExtensionstringThe relative path without extension.
extensionstringThe file extension.
Returns
- string
A relative path that doesn't exist.
GetDescendentPath(string)
Get descendent path if the path is descendent of current path; otherwise
return path.
public static string GetDescendentPath(string path)
Parameters
pathstringpath
Returns
- string
descendent path if the
pathis descendent of current path; otherwise returnpath
GetFileSizeString(FileInfo, string)
Gets a formatted string representing the file size.
public static string GetFileSizeString(this FileInfo fileInfo, string format = "{0,6:###.00} {1,-2:##}")
Parameters
Returns
- string
A formatted string representing the file size.
IsBinaryFile(string)
Determines if a file is likely to be a binary file by checking for control characters.
public static bool IsBinaryFile(string filePath)
Parameters
filePathstringPath to the file to check
Returns
- bool
True if the file appears to be binary, false otherwise
ReadAllTextWithFileShareAsync(string)
Reads all text from a file with file sharing enabled.
public static Task<string> ReadAllTextWithFileShareAsync(string filePath)
Parameters
filePathstringThe path of the file to read.
Returns
ToIndexedFile(IEnumerable<string>)
Converts a collection of file paths to a collection of indexed files.
public static IEnumerable<IndexedFile> ToIndexedFile(this IEnumerable<string> files)
Parameters
filesIEnumerable<string>The collection of file paths.
Returns
- IEnumerable<IndexedFile>
A collection of indexed files.