In this article we will learn how to read and write a file using a StorageFile object.
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.Step1Include the following namespace in your .cs file:using Windows.Storage;Step 2The 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 3Now, 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 4You can get a StorageFile that represents readable files by the StorageFolder.GetFileAsync method.
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
Step 5Read text from a file by calling the ReadTextAsync methods of the FileIO class, as in:string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);Now we try to write or read bytes to/from a file using a buffer.Step 6Include a namespace in the .cs file, as in:using Windows.Security.Cryptography;Step 7Get a buffer of the bytes that you want to write to your file. Here you can use the CryptographicBuffer class to create a buffer:CryptographicBuffer.ConvertStringToBinary("What fools these mortals be", Windows.Security.Cryptography.BinaryStringEncoding.Utf8);Step 8Now, write these bytes from your buffer to a file by calling the WriteBufferAsync method of the FileIO class; see:await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);Writing or reading text to a file by using a stream.Step 9We need to open a stream over a file by calling the StorageFile.OpenAsync method. It will provide you a stream of the file's content when the open operation completes.var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);Step 10Get an output stream by calling the GetOutputStreamAt method from the stream. Now, you have to Write text to the outputStream by creating a new DataWriter object and calling the DataWriter.WriteString method.
using (var outputStream = stream.GetOutputStreamAt(0)) { DataWriter dataWriter = new DataWriter(outputStream);dataWriter.WriteString("My name is Gaurav."); } Step 11 Now you have to Save the text to file and close the stream by calling the writer.StoreAsync.await dataWriter.StoreAsync(); await outputStream.FlushAsync(); Summary Now, I think you should understand how to read and write from a file if you have a StorageFile that represents the file.
using (var outputStream = stream.GetOutputStreamAt(0)) { DataWriter dataWriter = new DataWriter(outputStream);dataWriter.WriteString("My name is Gaurav."); }
Step 11 Now you have to Save the text to file and close the stream by calling the writer.StoreAsync.
await dataWriter.StoreAsync(); await outputStream.FlushAsync();
Summary
Now, I think you should understand how to read and write from a file if you have a StorageFile that represents the file.
Microsoft's Windows AppStudio