简体   繁体   English

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

[英]Cross platform way to get directory for application data in .NET Core 1.1

At this moment I am writing a ASP.NET Core web application that should be able to run on Windows and Linux (Ubuntu 16.04). 目前,我正在编写一个ASP.NET Core Web应用程序,该应用程序应该能够在Windows和Linux(Ubuntu 16.04)上运行。 I have some data I want to store, but it is so little, using a database would be a huge waste of performance. 我有一些要存储的数据,但是数量很少,使用数据库会浪费大量的性能。 Not to mention the installation procedure would be twice as long. 更不用说安装过程的时间将是原来的两倍。

That's why I want to save this information in a file. 这就是为什么我要将这些信息保存在文件中的原因。 In .NET Framework I would use something like Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) to get a directory where I can store my application files. 在.NET Framework中,我将使用类似于Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)的目录来存储我的应用程序文件。 Unfortunately this method is not available in .NET Core 1.1 . 不幸的是,此方法在.NET Core 1.1中不可用

Is there any way to get a folder to write to, without hardcoding it? 有没有办法对文件夹进行写入而无需对其进行硬编码?

Below is an example of the data I want to write. 以下是我要写入的数据的示例。 There would only be about 5 devices at any time in this list. 此列表中的任何时间都只有大约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>

I usually go with a folder relative from the IHostingEnvironment.ContentRootPath . 我通常会使用IHostingEnvironment.ContentRootPath相对文件夹。 It provides a cross-platform way to access files from the root of your application. 它提供了一种跨平台的方法来从应用程序的根目录访问文件。 You could even call it App_Data if you'd like. 如果愿意,您甚至可以将其称为App_Data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM