File access sample

Shows how to create, read, write, copy and delete a file, how to retrieve file properties, and how to track a file or folder so that your app can access it again. This sample uses the Windows.Storage and Windows.Storage.AccessCache APIs.

Note: This sample is part of a large collection of UWP feature samples. You can download this sample as a standalone ZIP file from docs.microsoft.com, or you can download the entire collection as a single ZIP file, but be sure to unzip everything to access shared dependencies. For more info on working with the ZIP file, the samples collection, and GitHub, see Get the UWP samples from GitHub. For more samples, see the Samples portal on the Windows Dev Center.

The sample demonstrates these tasks:

  1. Create a file in the Pictures library

    Uses one of the StorageFolder.CreateFileAsync methods to create the file.

  2. Get a file's parent folder

    Uses the StorageFile.GetParentAsync method to get the parent folder of the file that was created in the Picture folder. The app has the Pictures library capability, so it can access the folder where the file was created.

  3. Write and read text in a file

    Uses the FileIO.WriteTextAsync and FileIO.ReadTextAsync methods to write and read the file. For more info, see Create, write, and read a file.

  4. Write and read bytes in a file

    Uses the FileIO.WriteBufferAsync and FileIO.ReadBufferAsync methods to write and read the file. For more info, see Create, write, and read a file.

  5. Write and read a file using a stream

    Uses the following API to write and read the file using a stream.

    For more info, see Create, write, and read a file.

  6. Display file properties

    Uses the StorageFile.GetBasicPropertiesAsync method and the StorageFile.Properties property to get the properties of the file.

  7. Track a file or folder so that you can access it later (persisting access)

    Uses the StorageApplicationPermissions.FutureAccessList and StorageApplicationPermissions.MostRecentlyUsedList properties to remember a file or folder so that it can be accessed later.

    For more info, see Track recently used files and folders.

  8. Copy a file

    Uses one of the StorageFile.CopyAsync methods to copy the file.

  9. Compare two files to see if they're the same

    Uses the StorageFile.IsEqual method to compare two files.

  10. Delete a file

    Uses one of the StorageFile.DeleteAsync methods to delete the file.

  11. Try to get a file without getting an error

    Uses the StorageFolder.TryGetItemAsync method to get a file without raising an exception.

Note The Windows universal samples require Visual Studio to build and Windows 10 to execute.

To obtain information about Windows 10 development, go to the Windows Dev Center

To obtain information about Microsoft Visual Studio and the tools for developing Windows apps, go to Visual Studio

Note*  If you want to learn about accessing files using a file picker, see Open files and folders with a picker.

Reference

Windows.Storage namespace
Windows.Storage.AccessCache namespace
Windows.Storage.FileProperties
Windows.Storage.Streams namespace

System requirements

  • Windows 10 build 10586 or higher

Build the sample

  1. If you download the samples ZIP, be sure to unzip the entire archive, not just the folder with the sample you want to build.
  2. Start Microsoft Visual Studio and select File > Open > Project/Solution.
  3. Starting in the folder where you unzipped the samples, go to the Samples subfolder, then the subfolder for this specific sample, then the subfolder for your preferred language (C++, C#, or JavaScript). Double-click the Visual Studio Solution (.sln) file.
  4. Press Ctrl+Shift+B, or select Build > Build Solution.

Run the sample

The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.

Deploying the sample

  • Select Build > Deploy Solution.

Deploying and running the sample

  • To debug the sample and then run it, press F5 or select Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or select Debug > Start Without Debugging.