文件、文件夹和库Files, folders, and libraries

使用 Windows.StorageWindows.Storage.StreamsWindows.Storage.Pickers 命名空间中的 API,以在文件中读取和写入文本和其他数据格式并管理文件和文件夹。You use the APIs in the Windows.Storage, Windows.Storage.Streams, and Windows.Storage.Pickers namespaces to read and write text and other data formats in files, and to manage files and folders. 在本部分中,你还将了解有关读取和写入应用设置、文件和文件夹选取器、特殊沙盒式位置(如视频/音乐库)的信息。In this section, you'll also learn about reading and writing app settings, about file and folder pickers, and about special sand-boxed locations such as the Video/Music library.

主题Topic 说明Description
枚举和查询文件和文件夹Enumerate and query files and folders 访问位于文件夹、库、设备或网络位置的文件和文件夹。Access files and folders in either a folder, library, device, or network location. 还可以通过构造文件和文件夹查询来查询某个位置的文件和文件夹。You can also query the files and folders in a location by constructing file and folder queries.
创建、写入和读取文件Create, write, and read a file 使用 StorageFile 对象读取和写入文件。Read and write a file using a StorageFile object.
向文件进行写入的最佳做法Best practices for writing to files 了解使用 FileIOPathIO 类的各种文件写入方法的最佳做法。Learn best practices for using various file writing methods of the FileIO and PathIO classes.
获取文件属性Get file properties 获取由 StorageFile 对象表示的文件的属性:顶级、基本和扩展属性。Get properties—top-level, basic, and extended—for a file represented by a StorageFile object.
使用选取器打开文件和文件夹Open files and folders with a picker 通过让用户与选取器交互来访问文件和文件夹。Access files and folders by letting the user interact with a picker. 可以使用 FolderPicker 来获取对文件夹的访问权限。You can use the FolderPicker to gain access to a folder.
使用选取器保存文件Save a file with a picker 使用 FileSavePicker 让用户指定名称和他们想让应用保存文件的位置。Use FileSavePicker to let users specify the name and location where they want your app to save a file.
访问家庭组内容Accessing HomeGroup content 访问存储在用户的“家庭组”文件夹中的内容,包括图片、音乐和视频。Access content stored in the user's HomeGroup folder, including pictures, music, and videos.
确定 Microsoft OneDrive 文件的可用性Determining availability of Microsoft OneDrive files 使用 StorageFile.IsAvailable 属性确定 Microsoft OneDrive 文件是否可用。Determine if a Microsoft OneDrive file is available using the StorageFile.IsAvailable property.
音乐、图片和视频库中的文件和文件夹Files and folders in the Music, Pictures, and Videos libraries 将现有的音乐、图片和视频文件夹添加到相应的库。Add existing folders of music, pictures, or videos to the corresponding libraries. 你还可以从库中删除文件夹、获取库中的文件夹列表,并发现存储的照片、音乐和视频。You can also remove folders from libraries, get the list of folders in a library, and discover stored photos, music, and videos.
跟踪最近使用的文件和文件夹Track recently used files and folders 通过将用户经常访问的文件添加到你的应用的最近使用列表 (MRU) 中来跟踪这些文件。Track files that your user accesses frequently by adding them to your app's most recently used list (MRU). 该平台会为你管理 MRU,它会根据各个项的上次访问时间对它们进行排序,并在列表中的项超过 25 个的限制时删除最旧的项。The platform manages the MRU for you by sorting items based on when they were last accessed, and by removing the oldest item when the list's 25-item limit is reached. 所有应用都有其各自的 MRU。All apps have their own MRU.
在后台跟踪文件系统更改Track file system changes in the background 跟踪对文件系统的更改,即使应用未运行。Track changes to the file system, even when the app isn't running.
访问 SD 卡Access the SD card 你可以在可选 MicroSD 卡上存储和访问不重要的数据,尤其是内部存储具有限制的低成本移动设备。You can store and access non-essential data on an optional microSD card, especially on low-cost mobile devices that have limited internal storage.
文件访问权限File access permissions 默认情况下,应用可以访问特定文件系统位置。Apps can access certain file system locations by default. 应用也可以通过文件选取器或声明功能访问其他位置。Apps can also access additional locations through the file picker, or by declaring capabilities.
在 UWP 中快速访问文件属性Fast access to file properties in UWP 高效地从库中收集文件及其属性的列表以用于 UWP 应用。Efficiently gather a list of files and their properties from a library to use in a UWP app.

文件夹枚举示例Folder enumeration sample

文件访问示例File access sample

文件选取器示例File picker sample