8

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>
Joris Molnar
  • 130
  • 1
  • 8
  • 1
    The `App_Data` folder doesn't really make sense in .Net Core on any platform anyway. – DavidG Mar 20 '17 at 21:29
  • try [this](http://stackoverflow.com/questions/39224518/path-to-localappdata-in-asp-net-core-application) – danatcofo Mar 20 '17 at 21:33
  • @DavidG In Ubuntu for example it is quite common for applications to save their files in `/var/lib`. I was hoping for some directory like that. – Joris Molnar Mar 20 '17 at 21:33
  • 2
    Why not use some folder relative from the `IHostingEnvironment.ContentRootPath`? – Henk Mollema Mar 20 '17 at 21:36
  • There is no common folder for appdata in Linux/macOS/Windows. You have to determine the platform and load from/save to the appropriate designated appdata directory. – bl4y. Mar 20 '17 at 21:40
  • 1
    `/var/lib` is a global store for all apps on your system, even if the method returned that as a value, it wouldn't represent the same thing as `App_Data`. I would say that you're much safer doing a `#if – DavidG Mar 20 '17 at 21:41
  • Thanks everyone. I am going to use @HenkMollema answer. If I run into any permission issues, I will try with platform directives. Could you post your comment as an answer so I can accept it? – Joris Molnar Mar 20 '17 at 21:46

1 Answers1

3

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.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • 2
    Thanks! To get my final path, I used `string settingsPath = Path.Combine(_hostingEnvironment.ContentRootPath, "AppData");` – Joris Molnar Mar 20 '17 at 21:58
  • 6
    What if I want to install an ASP.NET Core application under `C:\Program Files`? Would `ContentRootPath` not then also be somewhere under `C:\Program Files` i.e. somewhere that log files cannot be written without privileged access? I'd want my logs to be written to somewhere under %APPDATA% for Windows, `$HOME/.local/share/` under Unix, etc. – Tagc Jul 06 '17 at 09:49