1

I'm just writing my first .Net Core application and am knocking into an issue on OSX.

I started writing this on PC and am just running some unit tests on OSX.

This line:

var localAppData = Environment.GetEnvironmentVariable("LocalAppData");

Returned the expected value on PC but returns null on OSX. What would be the tidiest way to make this cross-platform?

Are there any additional considerations when applications are distributed in PKGs in terms of runtimes etc?

Many of the answers I'm finding in realtion to this talk about adding things to the launchd.conf which is all well and good for my development environment but what about when this is being run on a users machine?

What I'm trying is to find a way to retrieve some OSX equivalent of the Windows AppData.

A location where users applications can safely create files and store data that preconfigured on every OSX system.

Set
  • 47,577
  • 22
  • 132
  • 150
Jammer
  • 9,969
  • 11
  • 68
  • 115
  • Possible duplicate of [What is the cross-platform way of obtaining the path to the local application data directory?](https://stackoverflow.com/questions/11113974/what-is-the-cross-platform-way-of-obtaining-the-path-to-the-local-application-da) – Set Jul 22 '17 at 18:02
  • 1
    That's for Java. – Jammer Jul 22 '17 at 20:15
  • yes, but as the question more related to OS behavior, it well explains what path you may use. And it also has link to [What is the OSX equivalent to Window's AppData folder?](https://apple.stackexchange.com/questions/28928/what-is-the-osx-equivalent-to-windows-appdata-folder) – Set Jul 23 '17 at 08:09

1 Answers1

1

Here's what I'm using:

string GetLocalAppDataFolder() {
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        return Environment.GetEnvironmentVariable("LOCALAPPDATA");
    }
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
        return Environment.GetEnvironmentVariable("XDG_DATA_HOME") ?? Path.Combine(Environment.GetEnvironmentVariable("HOME"),".local","share");
    } 
    if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    {
        return Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Library", "Application Support");
    }
    throw new NotImplementedException("Unknown OS Platform");
}

The logic is as follows:

  • On Windows it returns the LOCALAPPDATA enviornment variable (usually something like C:\Users\{username}\AppData\Local).
  • On Linux it returns the XDG_DATA_HOME enviornment variable, or if that's not available the HOME variable appended with /.local/share (as per the FreeDesktop Base Directory Specification)
  • On MacOS it returns the HOME variable, appended with the path /Library/Application Support (so by default you'll end up with /Users/{username}/Library/Application Support
Chris HG
  • 1,412
  • 16
  • 20