File handling, start to finish
[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]
Learn how to add file-handling capabilities to your Windows Store app using C++, C#, or Visual Basic and running on Windows 8.1.
Each section of this topic describes a key file-handling feature, links to a topic that provides more detail on that feature, and offers info about how to quickly find the relevant code in this topic's companion File handling, start to finish companion sample.
Note Many of the methods used to interact with folders and files are asynchronous. You can learn how to write asynchronous Microsoft Visual C# apps in Quickstart: Using the await operator for asynchronous programming.
File access basics - enumerating, getting properties, and reading and writing data
Quickstart: Accessing files programmatically You can access files in a location such as a folder, library, device, or network location, with just two lines of code—a call to the StorageFolder.GetFilesAsync function. See Quickstart: Accessing files programmatically for step-by-step instructions that show how to perform tasks like enumerating the top-level files and folders of a specified location and querying files in a location. These steps show you how to enumerate files in a particular location:
This code example enumerates all files in Pictures. A Visual C# extension method is used so that you can easily query the files from any supported location.
This screen shot shows an example of enumerating the files in Pictures. Find it in the sample: The sample includes a page titled File Access Basics, which includes the examples presented in this section. The relevant Visual C# and XAML code is centralized to the FileAccessBasicsPage.xaml.cs ( |
|
Quickstart: Getting a file's properties File properties describe or quantify an attribute of a file or its contents. For example, file properties include data such as file name, path, file size, file attributes, and date last accessed. Quickstart: Getting a file's propertiesshows how to retrieve and display a file's top-level and basic properties. These steps show you how to get either top-level, basic, and extended file properties.
This code example enumerates all files in Pictures and displays various top-level, basic file, and extended properties.
This screen shot shows the results of querying for various file properties. Find it in the sample: The sample includes a page titled File Access Basics, which includes the examples presented in this section. All of the relevant Visual C# and XAML code is centralized to the FileAccessBasicsPage.xaml.cs ( |
|
Quickstart: Reading and writing a file A Windows Store app reads and writes files via the FileIO class. See Quickstart: Reading and writing a file for code examples that show how to read and write various types of data using the FileIO and StorageFile classes. Writing text to a file
Reading text from a file
This screen shot shows an example of running the sample and writing to the sample file. Find it in the sample: The sample includes a page titled File Access Basics, which includes the samples presented in this section. All of the relevant Visual C# and XAML code is centralized to the FileAccessBasicsPage.xaml.cs ( |
Working with file and folder pickers
Quickstart: Accessing files with file pickers Pickers—both file pickers and folder pickers—are used to display a list of files or folders from which users can select one or more items for further processing. Pickers can be configured programmatically to search for files and folders that match a specified filter (such as files with specific extensions), start at a particular folder, display a specific view mode (list or thumbnail), and much more. The following procedures illustrate how to configure a file or folder picker for various tasks: Configuring a single-file selection file picker
Configuring a multi-file select file picker
Configuring a folder picker
This screen shot shows the results of running the sample and selecting two files (sample1.png and sample2.png). Find it in the sample: The sample includes a page titled File Picker, which demonstrates the tasks outlined in this section (for both file and folder pickers). The Visual C# code and XAML for this sample is located in the FilePickerPage.xaml.cs and FilePickerPage.xaml files, respectively, along with helper methods in the FileHelper.cs file. |
Working with OneDrive files
Quickstart: Determining availability of OneDrive files Windows 8.1 enables users to mark OneDrive files as online only. When the user is disconnected from OneDrive, these files are not available. To help you programmatically determine the availability of a file, there's a new property called StorageFile.IsAvailable. These steps show you how to determine the availability of files using the StorageFile.IsAvailable property.
Find it in the sample: The sample includes a page titled OneDrive Files, which has a button that enumerates all of the files in the local machine's Pictures library. Each file name is displayed, along with its provider (This PC or OneDrive) and whether it is available. The Visual C# code and XAML for this sample are in the OneDriveFilesPage.xaml.cs and OneDriveFilesPage.xaml files, respectively, along with helper methods in the FileHelper.cs file. |
Finish up
Using the Windows App Certification Kit Recommended. Running the Windows App Certification Kit helps you make sure that your app fulfills Windows Store requirements. We recommend you run it whenever you add major functionality to your app. |
|
You’re finished! Now that you’ve explored different file-handling capabilities for your app, you're ready to create an app like the File handling, start to finish companion sample. |
Want to know more?
Quickstart: Using the await operator for asynchronous programming
Learn more about asynchronous programming in C#.
Quickstart: Accessing files programmatically
Learn more about enumerating files.
Quickstart: Getting a file's properties
Learn more about retrieving a file's top-level, basic, and extended properties.
Quickstart: Reading and writing a file
Learn more about reading and writing files.
Quickstart: Determining availability of OneDrive files
Learn more about determining whether a file is available.