How to create & read File txt with C# ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to create & read File txt with C# ?

5th Nov 2016, 11:52 AM
Mohamed Amine
Mohamed Amine - avatar
4 Answers
+ 3
class ReadFromFile { static void Main() { // The files used in this example are created in the topic // How to: Write to a Text File. You can change the path and // file name to substitute text files of your own. // Example #1 // Read the file as one string. string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt"); // Display the file contents to the console. Variable text is a string. System.Console.WriteLine("Contents of WriteText.txt = {0}", text); // Example #2 // Read each line of the file into a string array. Each element // of the array is one line of the file. string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt"); // Display the file contents by using a foreach loop. System.Console.WriteLine("Contents of WriteLines2.txt = "); foreach (string line in lines) { // Use a tab to indent each line of the file. Console.WriteLine("\t" + line); } // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } }
5th Nov 2016, 1:25 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 2
Do out want the code in JAVA or C#
5th Nov 2016, 12:15 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 1
for exemle in c++ : #include <fstream> int main(){ ofstream("C:/fichier.txt"); } in C# ?
5th Nov 2016, 12:31 PM
Mohamed Amine
Mohamed Amine - avatar
0
Thanks TechPro
5th Nov 2016, 1:29 PM
Mohamed Amine
Mohamed Amine - avatar