What is the correct way to get the iOS Library fol

2019-06-16 08:09发布

问题:

This will get me my iOS app's document root:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

Is there something similar to get to the Library folder?

回答1:

var docsPath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var libPath = Path.Combine (docsPath, "..", "Library");


回答2:

Currently (iOS 12) you can retrieve the Library path via:

var libraryPath = GetFolderPath(SpecialFolder.Resources); 

".../APP-UUID/Library"

For directories nested within the Library folder, such as the local app cache, you can do:

var pathToCache = Path.Combine(libraryPath, "Caches"); 

".../APP-UUID/Library/Caches"

Additional directories on Xamarin IOS that may be of interest:

SpecialFolder.Favorites =   ".../APP-UUID/Library/Favorites"
SpecialFolder.MyDocuments = ".../APP-UUID/Documents"
SpecialFolder.MyMusic =     ".../APP-UUID/Documents/Music"
SpecialFolder.MyPictures =  ".../APP-UUID/Documents/Pictures"
SpecialFolder.MyVideos =    ".../APP-UUID/Documents/Videos"
SpecialFolder.Desktop =     ".../APP-UUID/Documents/Desktop"

More options can be explored on the Xamarin Docs for File System



回答3:

In iOS8 I used this and it worked:

NSFileManager.DefaultManager.GetUrls (NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User) [0].Path

I got this from Xamarin's page about the iOS file system. It says "iOS 8 NOTE: parts of this document are affected by changes in iOS 8. If your application uses Environment.SpecialFolder or calculates relative paths like "../Documents" you should read this tech note from Apple. Updated information is also available at iOS File System Basics."