Joris Molnar
Joris Molnar

Reputation: 130

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>

Upvotes: 8

Views: 4776

Answers (1)

Henk Mollema
Henk Mollema

Reputation: 46641

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.

Upvotes: 3

Related Questions