Task 1 – File Input

The first piece of information we need is a text file’s name and location. Ask the user through the console what the file’s name and location are. After that, please ensure that the file does exist and at the given location. If the file does not exist or cannot be read, please return a message to the user stating that and ask them to enter another name and location.

Finally, if the file can be read then simply copy/read every line from the file. The contents of this file will be used in the next task.

Task 2 – File Output

Ask the user, through the console, what the name of the copy of the previous file should be called and where it should be saved. Check to see if a file with the same name already exists at the location given and if one does ask the user if they want to overwrite the file that already exists or if they would like to change the name and location of the new file. If they wish to change the name and location, go through the process of getting their input again through the console and checking if the newly given name and location are taken already too.

If they want to overwrite or no file already exists in the given location with the given name, then place the copied contents of the file that was read in the first task into this new file.

After successfully copying the contents of the first file into the second file, print to the console the name and location of both of those files.

A sample text file named "textfile.txt" has been supplied in case you need a file to test the above code on. The contents of that file is the declaration of independence.

Respuesta :

Answer:

Check the explanation

Explanation:

using System;

using System.IO;

                 

public class Program

{

 

  public static void Main()

  {

      string[] lines;

      Console.WriteLine("enter name of the file");

      String fileName=Console.ReadLine();

     

      Console.WriteLine("enter path of the file");

      String filePath=Console.ReadLine();

     

      using (StreamReader streamReader = File.OpenText(filePath))

      {

          String text = streamReader.ReadToEnd();

          lines = File.ReadAllLines(filePath);

      }

      Console.WriteLine("Enter path of the file to write");

      String outputFilePath = Console.ReadLine();

      bool fileExist = File.Exists(outputFilePath);

      if (fileExist)

      {      

                  overWriteOrChangeFileName(outputFilePath);          

      }

      else

      {

          File.Create(outputFilePath);

          File.WriteAllLines(outputFilePath, lines);

      }

      Console.Write("Input file Path"+filePath);

      Console.Write("output file path"+outputFilePath);

     

      void overWriteOrChangeFileName(String filePathtoCheck){

          Console.WriteLine("File exists. Do you want to overwrite the file or do you want to change the location and file name of the new file.Press Y to overwrite and press C to change the location and filename");

          String userInput=Console.ReadLine();

          if(userInput.Equals("Y")){

File.WriteAllLines(outputFilePath, lines);

          }else{

              Console.WriteLine("enter new path of the file including filename");

              String NewFileName=Console.ReadLine();

              outputFilePath=NewFileName;

              if(File.Exists(outputFilePath)){

                  overWriteOrChangeFileName(outputFilePath);                  

              }

              else

      {

          File.Create(outputFilePath);

          File.WriteAllLines(outputFilePath, lines);

      }

          }

      }

  }

 

}

ACCESS MORE