First, open the file by calling the StorageFile. OpenAsync method. It returns a stream of the file's content when the open operation completes. Next, get an output stream by calling the IRandomAccessStream. GetOutputStreamAt method from the stream. If you're using C , then enclose this in a using statement to manage the output stream's lifetime.
Now add this code if you're using C , within the existing using statement to write to the output stream by creating a new DataWriter object and calling the DataWriter.
WriteString method. Lastly, add this code if you're using C , within the inner using statement to save the text to your file with DataWriter.
StoreAsync and close the stream with IOutputStream. For additional details and best practice guidance, see Best practices for writing to files. Here's how to read from a file on disk using the StorageFile class. The common first step for each of the ways of reading from a file is to get the file with StorageFolder. Read text from your file by calling the FileIO. ReadTextAsync method. First, call the FileIO. ReadBufferAsync method. The following code uses the StreamReader class to open, to read, and to close the text file.
You can pass the path of a text file to the StreamReader constructor to open the file automatically. The ReadLine method reads each line of text, and increments the file pointer to the next line as it reads. When the ReadLine method reaches the end of the file, it returns a null reference.
For more information, see StreamReader Class. On the File menu, point to New , and then select Project. On the Debug menu, select Start to compile and to run the application. The Console window displays the contents of the Sample. The following code uses the StreamWriter class to open, to write, and to close the text file. In a similar way to the StreamReader class, you can pass the path of a text file to the StreamWriter constructor to open the file automatically. The WriteLine method writes a complete line of text to the text file.
This code creates a file that is named Test. OpenOrCreate, FileAccess. This will allow you to write in the file as well. Improve this answer. Parag Meshram Parag Meshram 7, 9 9 gold badges 48 48 silver badges 87 87 bronze badges. Add a comment. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks.
Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
0コメント