Today we will learn how to read and write files using the StorageFolder object to the System folder in Windows 8 Apps.
We can read or write a file in many ways such as using a buffer and using a stream we can write or read a file. In this article we will see all the aspects of reading and writing a file using the C# language in Windows 8 Apps.
We can access all system libraries by using the class known as KnownFolders. Depending on the user requirements and type of file the library can accessed using this class. The KnownFolder class provides the way to access the common location that contains user content or data. It provides the following locations:
KnownFolders.DocumentsLibrary;
KnownFolders.RemovableDevices;
KnownFolders.VideosLibrary;
KnownFolders.HomeGroup;
KnownFolders.MusicLibrary;
KnownFolders.PicturesLibrary;
In this article we will be using DocumentsLibrary to read or write a file.
Step1
Include the following namespace in your .cs file:
using Windows.Storage;
Step 2
The following is an example of getting the DocumentsLibrary folder where a file is to be created and then creating the example.txt file and storing the StorageFile object that is returned:
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
Note: In the preceding code I use the await keyword to create a file. You must use the await keyword in the declaration of method in which these code is used.
Step 3
Now, in this step I will show you how to write a file.
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "Swift as a shadow");
In the preceding code I use the FileIO class to write the text to a file by calling a method WriteTextAsync. It will take two arguments; the first is a varibale of StorageFolder class and the second is the text to be written.
Note: In the preceding code I use the Async method to write the text. You must use the await keyword in the declaration of method in which these code is used.
Step 4
You can get a StorageFile that represents readable files by the StorageFolder.GetFileAsync method.

Brahim MasmoudiPosted May 2, 2017, 4:27 AM
I want to know how to upload a file to a server in UWP project, can u help me ?