我正在开发一个使用 .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);
}
有任何想法吗?有更好的方法吗?如何获取当前目录?
Environment.CurrentDirectory
返回当前目录。 - VansFannel