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}\AppData\Local
之类的东西). - 在Linux上,它返回
XDG_DATA_HOME
环境变量,如果不可用,则返回HOME
变量并附加/.local/share
(根据
- On Windows it returns the
LOCALAPPDATA
enviornment variable (usually something likeC:\Users\{username}\AppData\Local
). - 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屋!