0

How to i write a c# programme to store data into a text file using C#?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Contact
{
    string Name;
    string Address;
    string Email;
    string PhoneNo;
    public void AddContact()
    { 
    Console.WriteLine("Enter contact name");
    Name = Console.ReadLine();
    Console.WriteLine("Enter contact Address");
    Address = Console.ReadLine();
    Console.WriteLine("Enter contact Email");
    Email = Console.ReadLine();
    Console.WriteLine("Enter contact PhoneNo");
    PhoneNo = Console.ReadLine();
    }
    public void DisplayContact()
    {
        Console.WriteLine("Name");
        Console.WriteLine("Address");
        Console.WriteLine("Email");
        Console.WriteLine("PhoneNo");

    }
    class Details
    {

   public void Main(string[] args)
    {
        Contact contact1 = New contact();
        contact1.AddContact();
        contact1.DisplayContact();



    }
}
2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by Mike Askew
0

Use the StreamWriter class.

using System.IO;

StreamWriter sw = new StreamWriter(FullFilePathHere);
sw.WriteLine("I have written a line to the above specified file.");
sw.Close();

Edited by Mike Askew

0

OK, how to i input those code into a file so when i add a contact, it write it the file/

0
  public void AddContact()
  { 
    Console.WriteLine("Enter contact name");
    Name = Console.ReadLine();
    Console.WriteLine("Enter contact Address");
    Address = Console.ReadLine();
    Console.WriteLine("Enter contact Email");
    Email = Console.ReadLine();
    Console.WriteLine("Enter contact PhoneNo");
    PhoneNo = Console.ReadLine();

    StreamWriter sw = new StreamWriter("FilePathHere");
    sw.WriteLine(string.Format("{0},{1},{2},{3},"), Name, Address, Email, PhoneNo);
    sw.Close();
  }

Would create a CSV style storage of the details.

Edited by Mike Askew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.