我正在开发一个.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);
}
任何想法?有更好的方法吗?我如何获得当前目录?
我认为这将产生作为他的第一线相同的结果,在'串currentDir ' – MethodMan 2014-12-01 17:35:05
谢谢,'Environment.CurrentDirectory'返回当前目录。 – VansFannel 2014-12-01 17:41:43
不客气@VansFannel – 2014-12-01 17:42:38