Question #38986

describe the different method of the classes that Elina would be using to implement file input and output operations in the application.

Expert's answer

Answer on Question#38986- Programming, C#

1. describe the different method of the classes that Elina would be using to implement file input and output operations in the application.

Solution.

using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";
        if (!File.Exists(path))
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
        }
        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
        try
        {
            string path2 = path + "temp";
            // Ensure that the target does not exist.
            File.Delete(path2);
            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine(" {0} was copied to {1}.", path, path2);
            // Delete the newly created file.
            File.Delete(path2);
            Console.WriteLine(" {0} was successfully deleted.", path2);
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS