در یک فایل بنویسید و آن را بخوانید(Write To a File and Read It)

در
به عنوان مثال، از روش WriteAllText() استفاده می کنیم.
فایلی با نام "filename.txt" ایجاد کنید و مقداری محتوا در آن بنویسید. سپس از ReadAllText() استفاده می کنیم.
روش خواندن محتویات فایل:



مثال


using System.IO;  // include the System.IO namespace

string writeText = "Hello World!"; // Create a text string
File.WriteAllText("filename.txt", writeText); // Create a file and write the content of writeText to it

string readText = File.ReadAllText("filename.txt"); // Read the contents of the file
Console.WriteLine(readText); // Output the content

The output will be:




Hello World!