stephen barry

stephen barry

  • NA
  • 4
  • 8.4k

Filestream

Jun 27 2013 5:29 PM
Pls how do search for record,edit a record and delete a record in text file using filestream in c-sharp.
This is what i have tried.pls help me out with a practical show.


      public void DELETE()
      {
         FileStream fs = new FileStream("PersonalAssistant.txt", FileMode.Open, FileAccess.Read, FileShare.Delete);
         //StreamReader sr = new StreamReader(fs);
         StreamReader sr = new StreamReader(fs);

         sr.BaseStream.Seek(0, SeekOrigin.Begin);

         string Receive = sr.ReadLine();


         Boolean found = false;

         Console.WriteLine("\nEnter the Name of contact that which to delete from the file");

         string SearchValue = Console.ReadLine();

         SearchValue = Receive;

         while ((Receive != null) && (found == false))
         {
            if (Receive == SearchValue)
            {
               found = true;
               SearchValue = Receive;
               File.Delete(SearchValue);
            }
            sr.Close();
            fs.Close();
         }
         Console.WriteLine("\nThe Data has been Deleted");
      }
















      public void Edit_Contact()
      {
         FileStream fs = new FileStream("PersonalAssistant.txt", FileMode.Append, FileAccess.Write, FileShare.Inheritable);
         //FileStream f=new FileStream("temp.txt");
         StreamWriter sr = new StreamWriter(fs);


         Console.WriteLine("\nHow Many Contact do which to modify");
         int Number = Convert.ToInt32(Console.ReadLine());

         for (int Index = 0; Index < Number; Index++)
         {
            Console.WriteLine("\nEnter the New Name for contact {0}", Index + 1);
            string Name = Console.ReadLine();

            Console.WriteLine("\nEnter the New Phone Number");
            double Phone = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("\nEnter the New Address for Contact");
            string Address = Console.ReadLine();
            //Display Blank Line
            Console.WriteLine();

            sr.WriteLine(Name);
            sr.WriteLine(Phone);
            sr.WriteLine(Address);

         }
         sr.Flush();
         sr.Close();
         fs.Close();
         Console.WriteLine("\nYou have Successfully Modified Your Record");

      }















      public void Search_Contact()
      {
         FileStream fs = new FileStream("PersonalAssistant.txt", FileMode.Open, FileAccess.Read);
         StreamReader sr = new StreamReader(fs);

         sr.BaseStream.Seek(0, SeekOrigin.Begin);


         string Name, Address;
         double phone;

         Name = sr.ReadLine();
         phone = sr.Read();
         Address = sr.ReadLine();

         Boolean found = false;

         Console.WriteLine("\nEnter The name of the Contact to Search for");
         string SearchName = Console.ReadLine();
         if ((Name == SearchName) || (found = false))
         {
            found = true;
            //SearchName = Name;
            Console.WriteLine("{0}", Name);
            //Name = sr.ReadLine();
            Console.WriteLine("{0}", phone);
            //phone = sr.Read();
            Console.WriteLine("{0}", Address);
            //Address = sr.ReadLine();
         }
         fs.Flush();
         sr.Close();
         fs.Close();

         Console.WriteLine("{0} ", SearchName + "Was not found");

      }
    
   

Answers (1)