支持读取内嵌的资源
This commit is contained in:
1 parent
4d389f4375
commit
8ef47830b6
1 file changed
+27
-17
+27
-17
@@ -6,17 +6,23 @@ public static class FileUtils {
|
|||||||
#region 读取
|
#region 读取
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 打开指定文件的只读 <see cref="FileStream"/>。
|
/// 打开指定文件的只读 <see cref="Stream"/>。
|
||||||
|
/// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static FileStream ReadAsStream(string filePath)
|
public static Stream ReadAsStream(string filePath, Type? type = null) {
|
||||||
=> ResilientUtils.RetryOn<IOException, FileStream>(()
|
if (type is null) {
|
||||||
=> new(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
|
return ResilientUtils.RetryOn<IOException, Stream>(() => new FileStream(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
|
||||||
|
} else {
|
||||||
|
return type.Assembly.GetManifestResourceStream(filePath) ?? throw new FileNotFoundException($"Embedded resource \"{filePath}\" was not found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读取文件中的所有内容。
|
/// 读取文件中的所有内容。
|
||||||
|
/// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static byte[] ReadAsBytes(string filePath) {
|
public static byte[] ReadAsBytes(string filePath, Type? type = null) {
|
||||||
using Stream fs = FileUtils.ReadAsStream(filePath); // 不能使用 File.ReadAllBytes,它不指定 FileShare.ReadWrite,会在文件被占用时抛出异常
|
using Stream fs = FileUtils.ReadAsStream(filePath, type); // 不能使用 File.ReadAllBytes,它不指定 FileShare.ReadWrite,会在文件被占用时抛出异常
|
||||||
using MemoryStream ms = new();
|
using MemoryStream ms = new();
|
||||||
fs.CopyTo(ms);
|
fs.CopyTo(ms);
|
||||||
return ms.ToArray();
|
return ms.ToArray();
|
||||||
@@ -24,17 +30,19 @@ public static class FileUtils {
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读取文件中的所有内容。
|
/// 读取文件中的所有内容。
|
||||||
|
/// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string ReadAsString(string filePath, Encoding? encoding = null)
|
public static string ReadAsString(string filePath, Encoding? encoding = null, Type? type = null)
|
||||||
=> FileUtils.ReadAsBytes(filePath).GetString(encoding);
|
=> FileUtils.ReadAsBytes(filePath, type).GetString(encoding);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读取文件中的所有内容。
|
/// 读取文件中的所有内容。
|
||||||
/// 若文件不存在或读取失败,返回 <see langword="null"/>,而不是抛出异常。
|
/// <para/>若文件不存在或读取失败,返回 <see langword="null"/>,而不是抛出异常。
|
||||||
|
/// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string? TryReadAsString(string filePath, Encoding? encoding = null) {
|
public static string? TryReadAsString(string filePath, Encoding? encoding = null, Type? type = null) {
|
||||||
try {
|
try {
|
||||||
if (!FileUtils.Exists(filePath)) return null;
|
if (type != null && !FileUtils.Exists(filePath)) return null;
|
||||||
return FileUtils.ReadAsBytes(filePath).GetString(encoding);
|
return FileUtils.ReadAsBytes(filePath, type).GetString(encoding);
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -42,15 +50,17 @@ public static class FileUtils {
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读取文件中的所有内容,并按行分割。
|
/// 读取文件中的所有内容,并按行分割。
|
||||||
|
/// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string[] ReadAsLines(string filePath, bool skipEmptyLines = false, Encoding? encoding = null)
|
public static string[] ReadAsLines(string filePath, bool skipEmptyLines = false, Encoding? encoding = null, Type? type = null)
|
||||||
=> FileUtils.ReadAsString(filePath, encoding).SplitLines(skipEmptyLines);
|
=> FileUtils.ReadAsString(filePath, encoding, type).SplitLines(skipEmptyLines);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读取文件中的所有内容,并按行分割。
|
/// 读取文件中的所有内容,并按行分割。
|
||||||
/// 若文件不存在或读取失败,返回空数组,而不是抛出异常。
|
/// <para/>若文件不存在或读取失败,返回空数组,而不是抛出异常。
|
||||||
|
/// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string[] TryReadAsLines(string filePath, bool skipEmptyLines = false, Encoding? encoding = null)
|
public static string[] TryReadAsLines(string filePath, bool skipEmptyLines = false, Encoding? encoding = null, Type? type = null)
|
||||||
=> FileUtils.TryReadAsString(filePath, encoding)?.SplitLines(skipEmptyLines) ?? [];
|
=> FileUtils.TryReadAsString(filePath, encoding, type)?.SplitLines(skipEmptyLines) ?? [];
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user