简体   繁体   English

OSX上LocalAppData环境变量的替代方法

[英]Alternative for LocalAppData environment variable on OSX

I'm just writing my first .Net Core application and am knocking into an issue on OSX. 我只是在编写我的第一个.Net Core应用程序,并且正在讨论OSX上的一个问题。

I started writing this on PC and am just running some unit tests on OSX. 我开始在PC上编写此代码,并且仅在OSX上运行一些单元测试。

This line: 这行:

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

Returned the expected value on PC but returns null on OSX. 在PC上返回了期望值,但在OSX上返回了null 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? 在运行时等方面将应用程序分发到PKG中时,是否还有其他考虑事项?

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? 我在这次演讲中找到了很多答案,这些话题涉及将内容添加到launchd.conf中,这对我的开发环境来说是非常好的,但是当它在用户计算机上运行时又如何呢?

What I'm trying is to find a way to retrieve some OSX equivalent of the Windows AppData. 我正在尝试找到一种方法来检索与Windows AppData类似的OSX。

A location where users applications can safely create files and store data that preconfigured on every OSX system. 用户应用程序可以在其中安全地创建文件并存储在每个OSX系统上预先配置的数据的位置。

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 ). 在Windows上,它返回LOCALAPPDATA环境变量(通常类似于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 ) 在Linux上,它返回XDG_DATA_HOME环境变量,或者如果该变量不可用,则HOME变量将附加/.local/share (根据FreeDesktop基本目录规范
  • 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 在MacOS上,它返回HOME变量,并附加路径/Library/Application Support (因此,默认情况下,您将以/Users/{username}/Library/Application Support结尾

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

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