【发布时间】:2021-08-23 17:49:37
【问题描述】:
我正在使用 .Net Framework 4.5.1 开发一个 C# 库,以便在 Windows 8.1 桌面应用程序中使用它。
在这个库项目中,我有一个JSON 文件,我想加载它。首先,我试图用这个来获取当前目录:
string currentDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
但是,我已经测试过了,Assembly.GetEntryAssembly() 为空。
也许,我可以使用资源文件而不是 JSON 文件。
这是方法:
private void LoadData()
{
string currentDir =
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string file =
Path.Combine(currentDir, cardsDir, cardsFile);
string json =
File.ReadAllText(file);
Deck = JsonConvert.DeserializeObject<Card[]>(json);
}
有什么想法吗?有更好的方法吗?如何获取当前目录?
【问题讨论】:
标签: c# json wpf windows-8 windows-8.1