Class ProgramFileUtility
- Namespace
- Songhay
- Assembly
- SonghayCore.dll
A few static helper members for System.IO.
public static class ProgramFileUtility
- Inheritance
-
ProgramFileUtility
- Inherited Members
Methods
CopyToFileStream(Stream, string?)
Copies the specified Stream to a new FileStream of the specified output path.
public static void CopyToFileStream(Stream stream, string? outputPath)
Parameters
CopyToMemoryStream(Stream)
Copies the specified Stream to a new MemoryStream.
public static byte[] CopyToMemoryStream(Stream stream)
Parameters
Returns
- byte[]
CountParentDirectoryChars(string?)
Counts the parent directory chars.
public static int CountParentDirectoryChars(string? path)
Parameters
pathstringThe path.
Returns
Remarks
This method is useful when running GetParentDirectory(string?, int).
WARNING: call NormalizePath(string?) to prevent incorrectly returning 0
in cross-platform scenarios.
FindParentDirectory(string?, string?, int)
Finds the parent directory.
public static string? FindParentDirectory(string? path, string? parentName, int levels)
Parameters
Returns
FindParentDirectoryInfo(string?, string?, int)
Finds the parent DirectoryInfo.
public static DirectoryInfo? FindParentDirectoryInfo(string? path, string? parentName, int levels)
Parameters
Returns
GetCombinedPath(string?, string?)
Combines path and root based on the ambient value of DirectorySeparatorChar of the current OS.
public static string GetCombinedPath(string? root, string? path)
Parameters
Returns
Remarks
For detail, see: 📚 https://github.com/BryanWilhite/SonghayCore/issues/14 📚 https://github.com/BryanWilhite/SonghayCore/issues/32 📚 https://github.com/BryanWilhite/SonghayCore/issues/97
GetCombinedPath(string?, string?, bool)
Combines path and root based on the ambient value of DirectorySeparatorChar of the current OS.
public static string GetCombinedPath(string? root, string? path, bool fileIsExpected)
Parameters
rootstringThe root.
pathstringThe path.
fileIsExpectedboolwhen
truewill throw FileNotFoundException when combined path is not of a file; otherwise will throw DirectoryNotFoundException when combined path is not a directory
Returns
Remarks
For detail, see: 📚 https://github.com/BryanWilhite/SonghayCore/issues/14 📚 https://github.com/BryanWilhite/SonghayCore/issues/32 📚 https://github.com/BryanWilhite/SonghayCore/issues/97
GetEncodedString(string?)
Gets the UTF-8 encoded string from.
public static string GetEncodedString(string? utf16Value)
Parameters
utf16ValuestringThe raw value.
Returns
GetEncodedString(string?, Encoding?)
public static string GetEncodedString(string? utf16Value, Encoding? outputEncoding)
Parameters
Returns
Remarks
Unicode encoding is the UTF-16 encoding of strings in .NET. See: https://docs.microsoft.com/en-us/dotnet/api/system.text.unicodeencoding
GetParentDirectory(string?, int)
Gets the parent directory.
public static string? GetParentDirectory(string? path, int levels)
Parameters
Returns
Remarks
A recursive wrapper for GetParent(string).
GetParentDirectoryInfo(string?, int)
Gets the parent DirectoryInfo.
public static DirectoryInfo? GetParentDirectoryInfo(string? path, int levels)
Parameters
Returns
Remarks
A recursive wrapper for GetParent(string).
GetRelativePath(string?)
Gets the relative path from the specified file segment
without leading dots (.) or DirectorySeparatorChar chars.
public static string? GetRelativePath(string? fileSegment)
Parameters
fileSegmentstringThe file segment.
Returns
Remarks
This method is the equivalent of calling:
IsForwardSlashSystem()
Returns true when the current OS
uses forward-slash (/) paths or not.
public static bool IsForwardSlashSystem()
Returns
NormalizePath(string?)
Normalizes the specified path with respect to the ambient value of DirectorySeparatorChar.
public static string? NormalizePath(string? path)
Parameters
pathstringThe path.
Returns
ReadZipArchiveEntries(FileInfo?, Action<string>?)
Read zip archive entries.
public static void ReadZipArchiveEntries(FileInfo? archiveInfo, Action<string>? fileAction)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
fileActionAction<string>The action to take per text file.
Remarks
Use entriesProjector for any filtering or sorting.
ReadZipArchiveEntries(FileInfo?, Action<string>?, Func<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>?)
Read zip archive entries.
public static void ReadZipArchiveEntries(FileInfo? archiveInfo, Action<string>? fileAction, Func<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>? entriesProjector)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
fileActionAction<string>The action to take per text file.
entriesProjectorFunc<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>The entries projector for LINQ filtering/sorting.
Remarks
Use entriesProjector for any filtering or sorting.
ReadZipArchiveEntriesByLine(FileInfo?, Action<int, string>?)
Read zip archive entries as strings, line by line.
public static void ReadZipArchiveEntriesByLine(FileInfo? archiveInfo, Action<int, string>? lineAction)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
lineActionAction<int, string>The action to take per text file line.
Remarks
This member is designed for compressed text documents that are too large to load into memory.
The fileAction includes the line number and the current line.
ReadZipArchiveEntriesByLine(FileInfo?, Action<int, string>?, Func<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>?)
Read zip archive entries as strings, line by line.
public static void ReadZipArchiveEntriesByLine(FileInfo? archiveInfo, Action<int, string>? lineAction, Func<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>? entriesProjector)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
lineActionAction<int, string>The action to take per text file line.
entriesProjectorFunc<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>The entries projector for LINQ filtering/sorting.
Remarks
This member is designed for compressed text documents that are too large to load into memory.
The fileAction includes the line number and the current line.
RemoveBackslashPrefixes(string?)
Removes conventional Directory prefixes
for relative paths, e.g. ..</code> or .</code>
public static string? RemoveBackslashPrefixes(string? path)
Parameters
pathstringThe path.
Returns
RemoveConventionalPrefixes(string?)
Removes conventional Directory prefixes
for relative paths based on the ambient value
of DirectorySeparatorChar.
public static string? RemoveConventionalPrefixes(string? path)
Parameters
pathstringThe path.
Returns
RemoveForwardslashPrefixes(string?)
Removes conventional Directory prefixes
for relative paths, e.g. ../ or ./.
public static string? RemoveForwardslashPrefixes(string? path)
Parameters
pathstringThe path.
Returns
TrimLeadingDirectorySeparatorChars(string?)
Trims the leading directory separator chars.
public static string? TrimLeadingDirectorySeparatorChars(string? path)
Parameters
pathstringThe path.
Returns
Remarks
Trims leading AltDirectorySeparatorChar and/or DirectorySeparatorChar, formatting relative paths for Combine(string, string).
UseZipArchive(FileInfo?, Action<ZipArchive?>?)
Centralizes the use of ZipArchive in Read.
public static void UseZipArchive(FileInfo? archiveInfo, Action<ZipArchive?>? archiveAction)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
archiveActionAction<ZipArchive>The action to take for the ZIP archive in use.
UseZipArchive(FileInfo?, Action<ZipArchive?>?, ZipArchiveMode)
Centralizes the use of ZipArchive
public static void UseZipArchive(FileInfo? archiveInfo, Action<ZipArchive?>? archiveAction, ZipArchiveMode zipArchiveMode)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
archiveActionAction<ZipArchive>The action to take for the ZIP archive in use.
zipArchiveModeZipArchiveModeThe ZipArchiveMode.
UseZipArchiveEntries(FileInfo?, Action<ReadOnlyCollection<ZipArchiveEntry>>?)
Centralizes the use of ReadOnlyCollection<T>.
public static void UseZipArchiveEntries(FileInfo? archiveInfo, Action<ReadOnlyCollection<ZipArchiveEntry>>? entriesAction)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
entriesActionAction<ReadOnlyCollection<ZipArchiveEntry>>The action to take for ZIP archive entries.
UseZipArchiveEntries(FileInfo?, Action<ReadOnlyCollection<ZipArchiveEntry>>?, Func<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>?)
Centralizes the use of ReadOnlyCollection<T>.
public static void UseZipArchiveEntries(FileInfo? archiveInfo, Action<ReadOnlyCollection<ZipArchiveEntry>>? entriesAction, Func<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>? entriesProjector)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
entriesActionAction<ReadOnlyCollection<ZipArchiveEntry>>The action to take for ZIP archive entries.
entriesProjectorFunc<ReadOnlyCollection<ZipArchiveEntry>, IEnumerable<ZipArchiveEntry>>The entries projector for LINQ filtering/sorting.
WriteZipArchiveEntry(FileInfo?, FileInfo)
Write zip archive entry with Optimal.
public static void WriteZipArchiveEntry(FileInfo? archiveInfo, FileInfo fileInfo)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
fileInfoFileInfoThe file information for writing the entry.
WriteZipArchiveEntry(FileInfo?, FileInfo, CompressionLevel)
Write zip archive entry.
public static void WriteZipArchiveEntry(FileInfo? archiveInfo, FileInfo fileInfo, CompressionLevel compressionLevel)
Parameters
archiveInfoFileInfoThe ZIP archive FileInfo.
fileInfoFileInfoThe file information for writing the entry.
compressionLevelCompressionLevelThe CompressionLevel.