Mac Catalyst 上的系统特殊文件夹

GetFolderPath API 会返回由 Environment.SpecialFolder 枚举指定的系统特殊文件夹的路径。 但是,其行为在 macOS 和 Mac Catalyst 之间是不同的。 下表针对所有受支持的 Environment.SpecialFolder 枚举值对其在 macOS 和 Mac Catalyst 上的行为进行对比:

Environment.SpecialFolder 枚举值 macOS 上的返回路径 Mac Catalyst 上的返回路径
AdminTools
ApplicationData $首页/库/应用支持 $HOME/Documents/.config
CDBurning
CommonAdminTools
CommonApplicationData /usr/share /usr/share
CommonDesktopDirectory
CommonDocuments
CommonMusic
CommonOemLinks
CommonPictures
CommonProgramFiles
CommonProgramFilesX86
CommonPrograms
CommonStartMenu
CommonStartup
CommonTemplates /usr/share/templates /usr/share/templates
CommonVideos
Cookies
Desktop $HOME/Desktop $HOME/Documents/Desktop
DesktopDirectory $HOME/Desktop $HOME/Documents/Desktop
Favorites $HOME/Library/Favorites $HOME/Library/Favorites
Fonts $HOME/Library/Fonts $HOME/Documents/.fonts
History
InternetCache $HOME/Library/Caches $HOME/Library/Caches
LocalApplicationData $首页/库/应用支持 $HOME/Documents
LocalizedResources
MyComputer
MyDocuments $HOME/Documents $HOME/Documents
MyMusic $HOME/Music $HOME/Documents/Music
MyPictures $HOME/Pictures $HOME/Documents/Pictures
MyVideos $HOME/Movies $HOME/Documents/Videos
NetworkShortcuts
Personal $HOME/Documents $HOME/Documents
PrinterShortcuts
ProgramFiles /Applications $HOME/Applications
ProgramFilesX86
Programs
Recent
Resources $HOME/Library
SendTo
StartMenu
Startup
System /System
SystemX86
Templates $HOME/Templates $HOME/Documents/Templates
UserProfile $HOME $HOME
Windows

返回的 ApplicationDataDesktopDesktopDirectoryFontsLocalApplicationDataMyMusicMyPicturesMyVideosProgramFilesSystemTemplates 的系统路径在 macOS 和 Mac Catalyst 上是不同的。 这是因为在设计上,Mac Catalyst 与 iOS 的行为匹配。

重要

$HOME 路径在启用或禁用应用沙盒功能的应用之间有所不同。 在沙盒环境中,$HOME 解析为 /Users/<username>/Library/Containers/<bundle-id>/Data,而在非沙盒环境中,它将解析为 /Users/<username>。 有关功能的详细信息,请参阅 Mac Catalyst 功能

在 Mac Catalyst 上匹配 macOS 行为

如果需要匹配 macOS 应用行为并在 Mac Catalyst 上使用相同的系统路径,建议通过以下方式获取此类路径:

  • Environment.SpecialFolder.ApplicationData

    使用 new NSFileManager().GetUrls(NSSearchPathDirectory.ApplicationSupportDirectory, NSSearchPathDomain.User)[0].Path 而非 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.Desktop

    使用 new NSFileManager().GetUrls(NSSearchPathDirectory.DesktopDirectory, NSSearchPathDomain.User)[0].Path 而非 Environment.GetFolderPath(Environment.SpecialFolder.Desktop, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.DesktopDirectory

    使用 new NSFileManager().GetUrls(NSSearchPathDirectory.DesktopDirectory, NSSearchPathDomain.User)[0].Path 而非 Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.Fonts

    使用 Path.Combine(new NSFileManager().GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User)[0].Path, "Fonts") 而非 Environment.GetFolderPath(Environment.SpecialFolder.Fonts, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.LocalApplicationData

    使用 new NSFileManager().GetUrls(NSSearchPathDirectory.ApplicationSupportDirectory, NSSearchPathDomain.User)[0].Path 而非 Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.MyMusic

    使用 new NSFileManager().GetUrls(NSSearchPathDirectory.MusicDirectory, NSSearchPathDomain.User)[0].Path 而非 Environment.GetFolderPath(Environment.SpecialFolder.MyMusic, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.MyPictures

    使用 new NSFileManager().GetUrls(NSSearchPathDirectory.PicturesDirectory, NSSearchPathDomain.User)[0].Path 而非 Environment.GetFolderPath(Environment.SpecialFolder.MyPictures, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.MyVideos

    使用 new NSFileManager().GetUrls(NSSearchPathDirectory.MoviesDirectory, NSSearchPathDomain.User)[0].Path 而非 Environment.GetFolderPath(Environment.SpecialFolder.MyVideos, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.ProgramFiles

    使用 "/Applications" 而非 Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.System

    使用 "/System" 而非 Environment.GetFolderPath(Environment.SpecialFolder.System, Environment.SpecialFolderOption.None)

  • Environment.SpecialFolder.Templates

    使用 Path.Combine(NSFileManager.HomeDirectory, "Templates") 而非 Environment.GetFolderPath(Environment.SpecialFolder.Templates, Environment.SpecialFolderOption.None)