Environment.SystemDirectory 可以返回什么值?

问题描述 投票:0回答:1

根据this,Environment.SystemDirectory的签名为:

public static string SystemDirectory { get; }

显然这将返回一个字符串,但我的问题是,考虑到.Net Core现在运行的操作系统多种多样,是否有任何情况下返回值可能是空字符串?或者是否会(尽可能)保证返回值是非零长度字符串?

我问的原因是我看到这样的代码:

string path = Environment.SystemDirectory;
if (!string.IsNullOrEmpty(path))
{
  ...
}
c# .net-core environment-variables
1个回答
1
投票

Environment.SystemDirectory
应始终具有与
Environment.GetFolderPath(Environment.SpecialFolder.System, Environment.SpecialFolderOption.None)

相同的行为

Windows 上:
相当于 GetSystemDirectoryW 永远不为空

macOS 上:
总是回来

"/System"

在所有其他平台上:
总是回来

string.Empty

© www.soinside.com 2019 - 2024. All rights reserved.