IOHelper

Ecng.IO

Provides helper methods for file and directory operations.

方法

AddRelative
public static string AddRelative(string path, string relativePart)
result = IOHelper.AddRelative(path, relativePart)

Adds a relative segment to the current path and returns the fully qualified path.

path
The base path.
relativePart
The relative segment to add.

返回值: The combined full path.

EnumerateLines
public static IEnumerable<string> EnumerateLines(Stream stream, Encoding encoding, bool leaveOpen)
result = IOHelper.EnumerateLines(stream, encoding, leaveOpen)

Enumerates the lines in the stream using the specified encoding.

stream
The stream to read lines from.
encoding
The encoding to use when reading the stream. Defaults to UTF8 if null.
leaveOpen
Indicates whether to leave the stream open after reading.

返回值: An enumerable collection of strings, each representing a line from the stream.

GetActualBuffer
public static ArraySegment<byte> GetActualBuffer(MemoryStream stream)
result = IOHelper.GetActualBuffer(stream)

Gets the actual buffer of the MemoryStream that contains the written data.

stream
The MemoryStream to retrieve the buffer from.

返回值: An ArraySegment containing the actual data from the MemoryStream.

GetBuildTimestamp
public static DateTime GetBuildTimestamp(Assembly assembly)
result = IOHelper.GetBuildTimestamp(assembly)

Gets the build moment of the specified assembly, from the stamp inside it rather than from the file it was loaded from. A single file or NativeAOT build has no such file, so an assembly whose build writes the stamp can still be dated there.

assembly
The assembly.

返回值: The build moment, in UTC.

GetDiskFreeSpace
public static long GetDiskFreeSpace(string driveName)
result = IOHelper.GetDiskFreeSpace(driveName)

Gets the available free space on the specified drive.

driveName
The drive name (e.g., "C:").

返回值: The amount of free space in bytes.

GetRelativePath
public static string GetRelativePath(string fileFull, string folder)
result = IOHelper.GetRelativePath(fileFull, folder)

Gets the relative path from a folder to a file.

fileFull
The full file path.
folder
The base folder.

返回值: The relative file path.

GetTimestamp
public static DateTime GetTimestamp(Assembly assembly)
result = IOHelper.GetTimestamp(assembly)

Gets the timestamp of the specified assembly.

assembly
The assembly.

返回值: The timestamp when the assembly was built.

IsPathsEqual
public static bool IsPathsEqual(string path1, string path2)
result = IOHelper.IsPathsEqual(path1, path2)

Compares two file paths for equality after normalization.

path1
The first file path to compare.
path2
The second file path to compare.

返回值: True if both paths are equal; otherwise, false.

MakeFullPath
public static string MakeFullPath(string path, string baseDir)
result = IOHelper.MakeFullPath(path, baseDir)

Builds an absolute path from a potentially relative path.

path
A relative or absolute path.
baseDir
The base directory used when is not rooted.

返回值: If is already rooted, the same value is returned; otherwise the path combined with is returned.

NormalizePath
public static string NormalizePath(string path, CultureInfo culture)
result = IOHelper.NormalizePath(path, culture)

Normalizes the provided file path for comparison purposes and converts it to lowercase. Uses invariant culture for case conversion to ensure consistent behavior across all locales.

path
The file path to normalize.
culture
Ignored. Kept for backward compatibility. Always uses invariant culture.

返回值: The normalized and lowercased file path.

NormalizePathNoLowercase
public static string NormalizePathNoLowercase(string path)
result = IOHelper.NormalizePathNoLowercase(path)

Normalizes the provided file path for comparison purposes without converting to lowercase.

path
The file path to normalize.

返回值: The normalized file path, or null if the input is empty or whitespace.

Read
public static object Read(Stream stream, Type type, int size)
result = IOHelper.Read(stream, type, size)

Reads an object of the specified type from the stream using the provided size.

stream
The stream to read from.
type
The type of object to read.
size
The size in bytes to read.

返回值: The object read from the stream.

Read
public static object Read(Stream stream, Type type)
result = IOHelper.Read(stream, type)

Reads an object of the specified type from the stream.

stream
The stream to read from.
type
The type of object to read.

返回值: The object read from the stream.

ReadBytes
public static void ReadBytes(Stream stream, Memory<byte> buffer)
IOHelper.ReadBytes(stream, buffer)

Reads a specified number of bytes from a stream.

stream
The input stream.
buffer
The buffer to fill.
ReadBytes
public static byte[] ReadBytes(Stream stream, byte[] buffer, int len, int pos)
result = IOHelper.ReadBytes(stream, buffer, len, pos)

Reads a specified number of bytes from a stream.

stream
The input stream.
buffer
The buffer to fill.
len
The number of bytes to read.
pos
The position in the buffer to start filling.

返回值: The buffer containing the read bytes.

Read``1
public static T Read<T>(Stream stream)
result = IOHelper.Read(stream)

Reads an object of type T from the stream.

stream
The stream to read from.

返回值: The object read from the stream.

ToFullPath
public static string ToFullPath(string path)
result = IOHelper.ToFullPath(path)

Converts a relative or partial path to a fully qualified path.

path
The input path.

返回值: The absolute path.

ToFullPathIfNeed
public static string ToFullPathIfNeed(string path)
result = IOHelper.ToFullPathIfNeed(path)

Replaces the %Documents% variable in the path with the actual Documents folder path.

path
The path containing the %Documents% variable.

返回值: The fully qualified path with the Documents folder.

Truncate
public static void Truncate(StreamWriter writer)
IOHelper.Truncate(writer)

Truncates the underlying stream used by the StreamWriter by clearing its content.

writer
The StreamWriter whose stream is to be truncated.
TryParseBuildTimestamp
public static bool TryParseBuildTimestamp(string informationalVersion, DateTime timestamp)
result = IOHelper.TryParseBuildTimestamp(informationalVersion, timestamp)

Reads the build moment out of an informational version that carries one.

informationalVersion
The informational version, optionally followed by + and the source revision.
timestamp
The build moment, in UTC.

返回值: if the version carries a stamp.

WriteBytes
public static void WriteBytes(Stream stream, byte[] bytes, int len, int pos)
IOHelper.WriteBytes(stream, bytes, len, pos)

Writes the specified bytes to a stream.

stream
The output stream.
bytes
The byte array.
len
The number of bytes to write.
pos
The position in the array to start writing from.
WriteEx
public static void WriteEx(Stream stream, object value)
IOHelper.WriteEx(stream, value)

Writes an extended representation of the provided object to the stream, prefixing its length.

stream
The stream to write to.
value
The object to write.
WriteRaw
public static void WriteRaw(Stream stream, object value)
IOHelper.WriteRaw(stream, value)

Writes the raw byte representation of the provided object to the stream.

stream
The stream to write to.
value
The object to write. Its byte representation will be determined.
WriteRaw
public static void WriteRaw(Stream stream, byte[] buffer)
IOHelper.WriteRaw(stream, buffer)

Writes a raw byte array to the stream.

stream
The stream to write to.
buffer
The byte array to write.

字段

DocsVar
public const string DocsVar
value = IOHelper.DocsVar

The %Documents% variable.