FileManager
A convenient interface to the contents of the file system, and the primary means of interacting with it.
Declaration
class FileManagerMentioned in
Overview
A file manager object lets you examine the contents of the file system and make changes to it. The FileManager class provides convenient access to a shared file manager object that is suitable for most types of file-related manipulations. A file manager object is typically your primary mode of interaction with the file system. You use it to locate, create, copy, and move files and directories. You also use it to get information about a file or directory or change some of its attributes.
When specifying the location of files, you can use either NSURL or NSString objects. The use of the NSURL class is generally preferred for specifying file-system items because URLs can convert path information to a more efficient representation internally. You can also obtain a bookmark from an NSURL object, which is similar to an alias and offers a more sure way of locating the file or directory later.
If you are moving, copying, linking, or removing files or directories, you can use a delegate in conjunction with a file manager object to manage those operations. The delegate’s role is to affirm the operation and to decide whether to proceed when errors occur. In macOS 10.7 and later, the delegate must conform to the FileManagerDelegate protocol.
In iOS 5.0 and later and in macOS 10.7 and later, FileManager includes methods for managing items stored in iCloud. Files and directories tagged for cloud storage are synced to iCloud so that they can be made available to the user’s iOS devices and Macintosh computers. Changes to an item in one location are propagated to all other locations to ensure the items stay in sync.
Sync control
A package is a directory that the system presents as a single file to the person using the device. Apps with documents that contain multiple files can use packages to manage contents like media assets. In iOS 26 and macOS 26 and later, FileManager introduces methods for controlling how a file provider syncs these items. By pausing sync when your app opens a package and resuming when it closes, your app can prevent the file provider from changing the contents of the package in unexpected ways, which potentially leaves the document in an inconsistent state. You can also use this pause and resume API on regular “flat” files.
Threading considerations
The methods of the shared FileManager object can be called from multiple threads safely. However, if you use a delegate to receive notifications about the status of move, copy, remove, and link operations, you should create a unique instance of the file manager object, assign your delegate to that object, and use that file manager to initiate your operations.
Topics
Creating a file manager
Accessing user directories
homeDirectoryForCurrentUserNSHomeDirectory()NSUserName()NSFullUserName()homeDirectory(forUser:)NSHomeDirectoryForUser(_:)temporaryDirectoryNSTemporaryDirectory()
Locating system directories
url(for:in:appropriateFor:create:)urls(for:in:)NSSearchPathForDirectoriesInDomains(_:_:_:)NSOpenStepRootDirectory()
Locating application group container directories
Discovering directory contents
contentsOfDirectory(at:includingPropertiesForKeys:options:)contentsOfDirectory(atPath:)enumerator(at:includingPropertiesForKeys:options:errorHandler:)enumerator(atPath:)FileManager.DirectoryEnumeratormountedVolumeURLs(includingResourceValuesForKeys:options:)FileManager.VolumeEnumerationOptionssubpathsOfDirectory(atPath:)subpaths(atPath:)
Creating and deleting items
createDirectory(at:withIntermediateDirectories:attributes:)createDirectory(atPath:withIntermediateDirectories:attributes:)createFile(atPath:contents:attributes:)removeItem(at:)removeItem(atPath:)trashItem(at:resultingItemURL:)
Replacing items
replaceItemAt(_:withItemAt:backupItemName:options:)replaceItem(at:withItemAt:backupItemName:options:resultingItemURL:)FileManager.ItemReplacementOptions
Moving and copying items
Managing iCloud-based items
ubiquityIdentityTokenurl(forUbiquityContainerIdentifier:)isUbiquitousItem(at:)setUbiquitous(_:itemAt:destinationURL:)startDownloadingUbiquitousItem(at:)evictUbiquitousItem(at:)url(forPublishingUbiquitousItemAt:expiration:)
Accessing file provider services
Controlling file provider synchronization
NSFileManagerSupportedSyncControlspauseSyncForUbiquitousItem(at:completionHandler:)resumeSyncForUbiquitousItem(at:with:completionHandler:)NSFileManagerResumeSyncBehaviorfetchLatestRemoteVersionOfItem(at:completionHandler:)NSFileVersionuploadLocalVersionOfUbiquitousItem(at:withConflictResolutionPolicy:completionHandler:)NSFileManagerUploadLocalVersionConflictPolicy
Creating symbolic and hard links
createSymbolicLink(at:withDestinationURL:)createSymbolicLink(atPath:withDestinationPath:)linkItem(at:to:)linkItem(atPath:toPath:)destinationOfSymbolicLink(atPath:)
Determining access to files
fileExists(atPath:)fileExists(atPath:isDirectory:)isReadableFile(atPath:)isWritableFile(atPath:)isExecutableFile(atPath:)isDeletableFile(atPath:)
Getting and setting attributes
componentsToDisplay(forPath:)displayName(atPath:)attributesOfItem(atPath:)attributesOfFileSystem(forPath:)setAttributes(_:ofItemAtPath:)
Getting and comparing file contents
Getting the relationship between items
getRelationship(_:ofDirectoryAt:toItemAt:)getRelationship(_:of:in:toItemAt:)FileManager.URLRelationship
Converting file paths to strings
Managing the delegate
Managing the current directory
Unmounting volumes
unmountVolume(at:options:completionHandler:)FileManager.UnmountOptionsNSFileManagerUnmountDissentingProcessIdentifierErrorKey
Working with HFS file types
Determining resource fork support
Supporting Types
FileManager.DirectoryEnumerationOptionsFileManager.SearchPathDirectoryFileManager.SearchPathDomainMaskFileAttributeKeyFileAttributeTypeFileProtectionTypeURLFileProtection
Notifications
Deprecated Methods
changeFileAttributes(_:atPath:)fileAttributes(atPath:traverseLink:)fileSystemAttributes(atPath:)directoryContents(atPath:)createDirectory(atPath:attributes:)createSymbolicLink(atPath:pathContent:)pathContentOfSymbolicLink(atPath:)fileManager(_:shouldProceedAfterError:)fileManager(_:willProcessPath:)replaceItemAtURL(originalItemURL:withItemAtURL:backupItemName:options:)