C# 读取文件
作者:青柚1943
- 2022-10-24 湖南
本文字数:1967 字
阅读完需:约 6 分钟
1 当前项目下文件读取
private static async Task<string> ReaderLoacalMetadataAsync()
{
string filePath = $"{Directory.GetCurrentDirectory()}\\StaticFiles\\metadata.json";
if (!File.Exists(filePath))
return string.Empty;
using var streamReader = new StreamReader(filePath, Encoding.UTF8);
return await streamReader.ReadToEndAsync();
}
复制代码
2 程序集嵌入文件读取
2.1 项目文件下添加嵌入式资源
<ItemGroup>
<EmbeddedResource Include="StaticFiles\metadata.json" />
</ItemGroup>
复制代码
2.2 文件资源读取
//using System.Reflection;
/// <summary>
/// 读取本地文件资源
/// </summary>
/// <param name="folderName">目录名称</param>
/// <param name="fileName">文件名(不能包含“.”)</param>
/// <returns></returns>
private static async Task<string> ReadLoacalResourceAsync(string folderName, string fileName)
{
using var stream = ReadResource(folderName, fileName);
if (stream == null)
return string.Empty;
using var streamReader = new StreamReader(stream);
return await streamReader.ReadToEndAsync();
}
//目录为空 则直接拼文件名
//注:此处文件名不能包含“.”,否则Stream会返回null
private static Stream ReadResource(string folderName, string fileName)
{
var currentAssembly = Assembly.GetCallingAssembly();
//路径区分大小写
return currentAssembly.GetManifestResourceStream($"{currentAssembly.GetName().Name}.{folderName}.{fileName}");
}
复制代码
3* 树的序列化
3.1 Json 文件(metadata.json)
{
"uri": "metadata://bds",
"type": "app",
"label": "基础数据中心",
"children": [
{
"id": "first",
"uri": "metadata://bds/first",
"type": "module",
"label": "首营基础数据",
"children": [
{
"id": "product-browse",
"uri": "metadata://bds/first/product-browse",
"type": "route",
"path": "/product",
"label": "产品列表"
},
{
"id": "product-browse",
"uri": "metadata://bds/first/product-browse",
"type": "function",
"label": "浏览产品"
},
{
"id": "product-browse-all",
"uri": "metadata://bds/first/product-browse-all",
"type": "function",
"label": "浏览全局产品"
},
{
"id": "product-add",
"uri": "metadata://bds/first/product-add",
"type": "function",
"label": "添加产品"
},
{
"id": "product-delete",
"uri": "metadata://bds/first/product-delete",
"type": "function",
"label": "删除产品"
}
]
}
]
}
复制代码
3.2 树节点抽象
/// <summary>
/// 能力中心元数据节点
/// </summary>
public class MetadataNode
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("uri")]
public string URI { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("label")]
public string Label { get; set; }
[JsonPropertyName("path")]
public string Path { get; set; }
[JsonPropertyName("children")]
public List<MetadataNode> Children { get; set; } = new List<MetadataNode>();
}
复制代码
3.3 反序列化(System.Text.Json)
//using System.Text.Json;
public async Task<string> GetMetadataAsync()
{
var metadataTree = new MetadataNode()
{
Label = "根目录",
Type = "root"
};
var localMetadata = await ReadLoacalMetadata("StaticFiles", "metadata.json");
if (localMetadata != string.Empty)
{
var metadataNode = JsonSerializer.Deserialize<MetadataNode>(localMetadata) ?? new();
metadataTree.Children.Add(metadataNode);
}
//TODO:通过Dapr调用获取其他能力中心的元数据
return JsonSerializer.Serialize(metadataTree);
}
复制代码
3.4 前端效果
Tree
【字满发车】为什么要 50 个字才能发文呢。。。有点难受!
【字满发车】为什么要 50 个字才能发文呢。。。有点难受!
【字满发车】为什么要 50 个字才能发文呢。。。有点难受!
划线
评论
复制
发布于: 刚刚阅读数: 5
青柚1943
关注
生命不息,代码不止。 2020-08-04 加入
老街坊,小弄堂,是属于那年代白墙黑瓦的淡淡的忧伤。
评论