OSX 上 LocalAppData 环境变量的替代方案 [英] Alternative for LocalAppData environment variable on OSX
问题描述
我正在编写我的第一个 .Net Core 应用程序,并且在 OSX 上遇到了一个问题.
I'm just writing my first .Net Core application and am knocking into an issue on OSX.
我开始在 PC 上写这篇文章,只是在 OSX 上运行一些单元测试.
I started writing this on PC and am just running some unit tests on OSX.
这一行:
var localAppData = Environment.GetEnvironmentVariable("LocalAppData");
在 PC 上返回预期值,但在 OSX 上返回 null
.使这个跨平台的最整洁的方法是什么?
Returned the expected value on PC but returns null
on OSX. What would be the tidiest way to make this cross-platform?
在运行时等方面,在 PKG 中分发应用程序时是否有任何其他注意事项?
Are there any additional considerations when applications are distributed in PKGs in terms of runtimes etc?
我在这个讨论中找到的许多答案都是关于将内容添加到 launchd.conf 这对我的开发环境来说都是很好的,但是当它在用户机器上运行时呢?
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?
我正在尝试寻找一种方法来检索某些与 Windows AppData 等效的 OSX.
What I'm trying is to find a way to retrieve some OSX equivalent of the Windows AppData.
用户应用程序可以安全地创建文件和存储在每个 OSX 系统上预配置的数据的位置.
A location where users applications can safely create files and store data that preconfigured on every OSX system.
推荐答案
这是我正在使用的:
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");
}
逻辑如下:
- 在 Windows 上,它返回
LOCALAPPDATA
环境变量(通常类似于C:Users{username}AppDataLocal
). - 在 Linux 上,它返回
XDG_DATA_HOME
环境变量,或者如果它不可用,则HOME
变量附加有/.local/share
(如根据 FreeDesktop 基本目录规范) - 在 MacOS 上,它返回
HOME
变量,附加路径/Library/Application Support
(因此默认情况下,您将以/Users/{username}/Library/Application Support
- On Windows it returns the
LOCALAPPDATA
enviornment variable (usually something likeC:Users{username}AppDataLocal
). - On Linux it returns the
XDG_DATA_HOME
enviornment variable, or if that's not available theHOME
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
这篇关于OSX 上 LocalAppData 环境变量的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!