File Operations in the Windows 8 Metro style app are included in the windows. Storage namespace, including storagefolder, storagefile, fileio, and other class libraries.
The file object is implemented using storagefile, and the file header is implemented using storagefolder. The following describes the specific usage and attributes of different libraries.
Create a file
StorageFolder storageFolder=KnownFolders.DocumentsLibrary; StorageFile storageFile=await storageFolder.CreateFileAsync("sample.txt",CreationCollisionOption.ReplaceExisting);
Knownfolders provides common file paths in the system.
Creationcollisionoption provides operation options for creating file conflicts.
The fileio object is responsible for reading/writing files.
Read files
string fielContent=await FileIO.ReadTextAsync(storageFile);
There are three read operations:
Returns the common text readtextasync (storagefile)/readtextasync (storagefile, unicodeencoding) (returns the specified text encoding format)
Returned stream readbufferasync (storagefile)
IBuffer buffer = await FileIO.ReadBufferAsync(storageFile); using (DataReader dataReader = DataReader.FromBuffer(buffer)) { string fileContent = dataReader.ReadString (buffer.Length); }
File content can also be read through the over-current
using (IRandomAccessStream readStream = await sampleFile.OpenAsync(FileAccessMode.Read)) { using (DataReader dataReader = new DataReader(readStream)) { uint numBytesLoaded = await dataReader.LoadAsync((uint)readStream.Size); string fileContent = dataReader.ReadString(numBytesLoaded); } }
Returns the text lines readlinesasync (storagefile)/readlinesasync (storagefile, unicodeencoding)
Write files
await FileIO.WriteTextAsync(storageFile,content)
Writing a file also includes the following operations:
Write buffer data writebufferasync (istorage file, ibuffer buffer)
Write bytes byte data writebytesasync (istorage file, bytes [] Byte)
Write writelinesasync (istoragefile file, iiterable (string)/writelinesasync (istoragefile file, iiterable (string), unicodeencoding)
Write the string writetextasync (istoragefile file, string content)
Write content through stream
using (IRandomAccessStream writeStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) { using (DataWriter dataWriter = new DataWriter(writeStream)) { dataWriter.WriteString(userContent); await dataWriter.StoreAsync(); await dataWriter.FlushAsync(); } }
The append text method is also provided.
Appendlinesasync (istoragefile, iiterable (string)/appendlinesasync (istoragefile, iiterable (string), unicodeencoding)
Appendtextasync (istoragefile, string)/appendtextasync (istoragefile, String, unicodeencoding)
Delete an object
await storageFile.DeleteAsync()
Copy a file
StorageFile storageFileCopy = await storageFile.CopyAsync(KnownFolders.DocumentsLibrary, "sample - Copy.txt", NameCollisionOption.ReplaceExisting);
There are also several ways to copy text
Copyandreplaceasync
Copyasync (istoragefolder)
Copyasync (istoragefolder, string filename)
Copyasync (istoragefolder, string filename, namecollisionoption)
Rename a file
StorageFile storageFile1=await storageFile.RenameAsync("sampleRe.txt")
Move files
await storageFile.MoveAsync(StorageFolder NewStorageFolder,string NewFileName);
When it comes to file operations, you have to talk about file streams.
In previous development, we often used operations such as file streams and byte arrays. In winrt, there are some differences among file streams such as irandomaccessstream, fileinputstream, fileoutstream, and filerandomaccessstream, use ibuffer to replace byte []. Let's take a look at the specific usage:
Get file stream
IRandomAccessStream readStream = await sampleFile.OpenAsync(FileAccessMode.Read)FileInputStream inputStream=readStream.GetInputStreamAt(0) as FileInputStream;FileOutputStream outStream=readStream.GetOutputStreamAt(0) as FileOutputStream;
For usage of datareader/datawriter, see
Conversion between ibuffer, byte [], and stream
IBuffer buffer = await FileIO.ReadBufferAsync(sampleFile);byte[] bytes=WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length);Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);
There are still many file operations that are not organized. Although winrt provides many file streams, their core usage is similar, we hope to gradually access the use of different types of streams in the future.