How to read the file content in c#




















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. Open Test. Unlike the previous example, this code passes two additional parameters to the constructor. The first parameter is the file path and the file name of the file.

The second parameter, true , specifies that the file is opened in append mode. If you specify false for the second parameter, the contents of the file are overwritten each time you run the code.

The third parameter specifies Unicode , so that StreamWriter encodes the file in Unicode format. For example, the file myFile. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? To test this code, find a text file or create one with some text in it on your machine and change the "textFile" variable to the full path of your.

View All. Mahesh Chand Updated date Feb 04, One more way to read a text file is using a StreamReader class that implements a TextReader and reads characters from a byte stream in a particular encoding. The complete code sample uses the above-discussed methods to read a text file and display its content to the console.

Next Recommended Reading. Net Core 6. Create A.



0コメント

  • 1000 / 1000