Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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. Unfortunately this method is not available in .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.

<devices>
    <device>
        <id>0</id>
        <name>xxx</name>
        <physicaladdress>yyyyyyyyy</physicaladdress>
    </device>
    ...
    <device>
        <id>5</id>
        <name>xxx</name>
        <physicaladdress>yyyyyyyyy</physicaladdress>
    </device>
</devices>
like image 672
Joris Molnar Avatar asked Mar 20 '17 21:03

Joris Molnar


People also ask

Is NET Core app supporting cross-platform?

NET Core is cross-platform. It runs on Windows, OS X and multiple distributions of Linux. It also supports different CPU architectures. We're adding more Linux distribution and CPU architecture support with the eventual goal of .

How is ASP.NET Core cross-platform?

So, to the "cross-platform", it means the server side, instead of the client side (access the web page via browser). For the . Net framework application, it runs/host primarily on Microsoft Windows. But Asp.net core application can run/host on Windows, OS X, and multiple distributions of Linux.

What is Kestrel in .NET Core?

Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the web server that's included and enabled by default in ASP.NET Core project templates. Kestrel supports the following scenarios: HTTPS. HTTP/2 (except on macOS†)


1 Answers

I usually go with a folder relative from the 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.

like image 82
Henk Mollema Avatar answered Sep 22 '22 14:09

Henk Mollema