using System; using System.IO; class Program { static void Main(string[] args) { string filePath = "example.txt"; string[] lines = File.ReadAllLines(filePath); for (int i = 0; i < lines.Length; i++) { lines[i] = lines[i].ToUpper(); } File.WriteAllLines(filePath, lines); Console.WriteLine("Modified file contents:"); Console.WriteLine(string.Join(Environment.NewLine, lines)); } }This example reads the contents of a text file into an array of strings, converts each string to uppercase, writes the modified contents back to the file, and outputs the modified contents to the console. The following steps are performed:
- Define a string variable
filePath
with the value “example.txt”.- Use the
File.ReadAllLines()
method to read the contents of the file atfilePath
into an array of stringslines
.- Use a
for
loop to iterate over each element oflines
and convert it to uppercase using theToUpper()
method.- Use the
File.WriteAllLines()
method to write the modified contents oflines
back to the file atfilePath
.- Output the modified contents of
lines
to the console usingConsole.WriteLine()
.Note that there are many other types of file operations you can perform, such as creating, copying, and deleting files, as well as working with other file formats such as binary files and XML files. This example is just one possible multi-step process for working with files.