Question #37966

The user should be able to save data to the customer details file. Write the code that Elina
should use to save data to the customer details file

Expert's answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Question
{
    class Program
    {
        static void Main(string[] args)
        {
            string Data = "";
            Console.WriteLine("Please enter some data to write into a file:");
            Data = Console.ReadLine();//Getting any input information from console
            string pass = @"D:\"; //Pass to the file
            string fileName = "customerDetailsFile.txt"; //file Name
            FileStream fs; //object to write data
            if (!File.Exists(pass + fileName)) //File exist?
            {
                Console.Write(pass + fileName + " doesn't exist. We will create it.");
                fs = new FileStream(pass + fileName, FileMode.Create, FileAccess.Write);
                //Create file for writing
            }
            else fs = new FileStream(pass + fileName, FileMode.Open, FileAccess.Write); // Just
            open it
            Byte[] info = new UTF8Encoding(true).GetBytes(Data); //Convert your data to Bytes
            fs.Seek(0, SeekOrigin.End); //Seeking for the last point in the file
            fs.Write(info, 0, info.Length); // Writing data
            fs.Close();//Do not forget to close a file
        }
    }
}

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