跨平台方式获取.NET Core 1.1中的应用程序数据目录

时间:2017-03-20 21:26:53

标签: c# asp.net-core cross-platform .net-core

目前我正在编写一个能够在Windows和Linux(Ubuntu 16.04)上运行的ASP.NET核心Web应用程序。我有一些我想要存储的数据,但它很少,使用数据库会大大浪费性能。更不用说安装程序会是两倍。

这就是我想将这些信息保存在文件中的原因。在.NET Framework中,我会使用Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)之类的东西来获取我可以存储应用程序文件的目录。 Unfortunately this method is not available in .NET Core 1.1

有没有办法让文件夹写入,而不需要硬编码?

以下是我想写的数据示例。此列表中任何时候只有大约5台设备。

<devices>
    <device>
        <id>0</id>
        <name>xxx</name>
        <physicaladdress>yyyyyyyyy</physicaladdress>
    </device>
    ...
    <device>
        <id>5</id>
        <name>xxx</name>
        <physicaladdress>yyyyyyyyy</physicaladdress>
    </device>
</devices>

1 个答案:

答案 0 :(得分:3)

我通常使用相对于IHostingEnvironment.ContentRootPath的文件夹。它提供了一种跨平台的方式来从应用程序的根目录访问文件。如果您愿意,甚至可以将其称为App_Data