4
Answers

filestream in c#

stephen barry

stephen barry

11y
3.7k
1
Please help me with this prblem;
The Project name of this, is called "PERSONAL ASSISTANT"
It is aim helping users Add,Edit,Search and DELETE RECORDS FROM FILES.

here is what i have tried so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace NiitProject
{
class PersonalAssistant
{
public void ShowIntro()
{
//Guide to the user

Console.Write("\nHELLO");

Console.WriteLine("\n\t\t ************** Your are Welcome!!! *************** ");

Console.WriteLine("\nTo Your ***PERSONAL ASSISTANT PROGRAM*** ");

Console.Write("This Program is Aim at helping you store Contact Details and Details of meetings");
Console.Write("\\Appointments.");

Console.WriteLine("\n\nKindly Enter Letter \\m\\ to view the Main Menu");

//To catch a user Defined Exception
try
{
char key = Convert.ToChar(Console.ReadLine());
}
catch (FormatException e)
{
//Message To be Displayed when tried
Console.WriteLine(e.Message);
}

Console.WriteLine("\n\t\t ***** MAIN MENU *****");

Console.WriteLine("\n(1.) Contact");

Console.WriteLine("\n(2.) Meetings\\Appointment");

Console.WriteLine("\nPress 1 OR 2 to Select any of the above Operations");

Main_Menu_Condition_For_Contact();
}

public void Main_Menu_Condition_For_Contact()//condition for Each Menu
{
try
{
//variable used for Menu Selection
int InputNum = Convert.ToInt32(Console.ReadLine());

//Input Validation Condition
while ((InputNum < 1) || (InputNum > 2))
{
//Input Validation Message
Console.WriteLine("\nINVALID NUMBER ENTERED.Before You can continue Enter 1 OR 2");

//Repeat Input Number
InputNum = Convert.ToInt32(Console.ReadLine());
}

//condition for Contact Menu
if (InputNum == 1)
{
//SubMenu List1
Console.Write("\n\t\t*****SUBMENUS FOR CONTACT*****");
Console.WriteLine("\n(1.) Add new Contact");
Console.WriteLine("\n(2.) Delete Contact");
Console.WriteLine("\n(3.) Edit Contact");
Console.WriteLine("\n(4.) Search Contact");

Console.WriteLine("\nPress 1,2,3 OR 4 to Select any of the above Operations");

//SubMenu Method for contact is been Called
SubMenuCondition_ForContact();
}
else if (InputNum == 2)
{
//Submenu List1 for Meeting
Console.Write("\n\t\t***** SUBMENUS FOR MEETING//APPOINTMENT *****");
Console.WriteLine("\n(1.) Add an Appointment ");
Console.WriteLine("\n(2.) Delete an Appointment");
Console.WriteLine("\n(3.) Edit an Appointment");
Console.WriteLine("\n(4.) Search for an Appointment");

//Message to prompt user to enter the correct value
Console.WriteLine("\nPress 1,2,3 OR 4 to Select any of the above Operations");

//SubMenu Method for Appointment is been Called
Sub_Menu_Condition_4_Meeting();
}

}
//User Defined Exception
catch (FormatException E)
{
//To Display Exception Message
Console.WriteLine(E.Message);
}
}
public void SubMenuCondition_ForContact()//Conditions for SubMenu
{

//Variable to Accept A Valid Value
int InputMenu2 = Convert.ToInt32(Console.ReadLine());

//Input Validation Condition

while ((InputMenu2 < 1) || (InputMenu2 > 4))
{
Console.WriteLine("\nINVALID NUMBER ENTERED.Before You can continue Enter 1,2,3 OR 4");
//Variable to accept a Value
InputMenu2 = Convert.ToInt32(Console.ReadLine());
}
//Statement to Carry out operation from any of the contact SubMenu Options

//int InputSelectionForContact;
switch (InputMenu2)
{
case 1:
{
AddContact();
}
break;
case 2:
{
DELETE();
}
break;
case 3:
{
Edit_Contact();
}
break;
case 4:
{
Search_Contact();
}
break;
default:
Console.WriteLine("\nNo operation for that value");
break;
}
}

public void AddContact()
{
FileStream fs = new FileStream("PersonalAssistant.txt", FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs);

Console.WriteLine("\nHow many Contacts do want to Add");
int Add = Convert.ToInt32(Console.ReadLine());

string NameContact;
double ContactPhone;
string Address;
for (int Index = 0; Index < Add; Index++)
{
Console.WriteLine("\nEnter the Name of Contact {0}", Index + 1);
NameContact = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("\nEnter the Phone Number ");
ContactPhone = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("\nEnter the Contact Address");
Address = Console.ReadLine();
//Leave A BlankLine
Console.WriteLine();

//Writes Contacts To File
writer.Write(NameContact);

//Write PhoneNumber To file
writer.Write(ContactPhone);

//Console.WriteLine();
writer.Write(Address);
Console.WriteLine();
Console.WriteLine("\nYour Contact Has been Added");
}
writer.Flush();
writer.Close();
fs.Close();
}
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");

}
public void Sub_Menu_Condition_4_Meeting()
{
//Variable to Accept A Valid Value
int InputMenu2 = Convert.ToInt32(Console.ReadLine());

//Input Validation Condition

while ((InputMenu2 < 1) || (InputMenu2 > 4))
{
Console.WriteLine("\nINVALID NUMBER ENTERED.Before You can continue Enter 1,2,3 OR 4");
//Variable to accept a Value
InputMenu2 = Convert.ToInt32(Console.ReadLine());
}
//Statement to Carry out operation from any of the SubMenu Options

//int InputSelectionForContact;
switch (InputMenu2)
{

case 1:
{
Add_Meeting();
}
break;
case 2:
{
DELETE_Meeting();
}
break;
case 3:
{
Edit_Meeting();
}
break;
case 4:
{
Search_Meeting();
}
break;
default:
Console.WriteLine("\nNo operation for that value");
break;

}


}

public void Add_Meeting()
{
FileStream fs = new FileStream("Meeting.txt", FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs);

Console.WriteLine("\nHow many meeting(s) do want to Add");
int Add = Convert.ToInt32(Console.ReadLine());


string Meeting_Subject;
string Meeting_Address;
double Meeting_DATE;
for (int Index = 0; Index < Add; Index++)
{
Console.WriteLine("\nEnter the Subject of Meeting {0}", Index + 1);
Meeting_Subject = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("\nEnter the Meeting Address ");
Meeting_Address = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("\nEnter the Date of the Meeting");
Meeting_DATE = Convert.ToDouble(Console.ReadLine());
//Leave A BlankLine
Console.WriteLine();

//Writes MeetingSubject To File
writer.Write(Meeting_Subject);

//Write Meeting Address To file
writer.Write(Meeting_Address);

//Write Meeting Date to file
writer.Write(Meeting_DATE);
Console.WriteLine();

Console.WriteLine("\nYour Meeting Has been Added");
}
writer.Flush();
writer.Close();
fs.Close();
}
public void DELETE_Meeting()
{
FileStream fs = new FileStream("Meeting.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 Subject of the meeting 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 Meeting has been Deleted");
}

public void Edit_Meeting()
{
FileStream fs = new FileStream("Meeting.txt", FileMode.Append, FileAccess.Write);
//FileStream f=new FileStream("temp.txt");
StreamWriter sr = new StreamWriter(fs);



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

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

Console.WriteLine("\nEnter The New Date For Meeting");
double New_Meeting_DATE = Convert.ToDouble(Console.ReadLine());

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

sr.WriteLine(New_Meeting_Subject);
sr.WriteLine(New_Meeting_DATE);
sr.WriteLine(New_Meeting_Address);

}
sr.Flush();
sr.Close();
fs.Close();
}
public void Search_Meeting()
{
FileStream fs = new FileStream("Meeting.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);

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

string Meeting_Subject;
string Meeting_Address;
double Meeting_DATE;

Meeting_Subject = sr.ReadLine();
Meeting_DATE = sr.Read();
Meeting_Address = sr.ReadLine();

//Boolean found = false;

Console.WriteLine("\nEnter the Name of the Meeting to Search for");
string SearchMeeting = Console.ReadLine();

while (Meeting_Subject != null)
{
if (Meeting_Subject == SearchMeeting)
{
//found = true;
SearchMeeting = Meeting_Subject;
Console.WriteLine(Meeting_Subject);
Meeting_Subject = sr.ReadLine();
Console.WriteLine(Meeting_Address);
Meeting_Address = sr.ReadLine();
Console.WriteLine(Meeting_DATE);
Meeting_DATE = sr.Read();

fs.Flush();
sr.Close();
fs.Close();
}
else
{
Console.WriteLine("{0} ", SearchMeeting + "Was not found");
}

}
}
public static void Main(string[] args)
{
PersonalAssistant ps = new PersonalAssistant();
string keepgoing = "yes";
do
{
ps.ShowIntro();
Console.WriteLine("\nWill You Like To Perform Another Operation.");
Console.WriteLine("\nEnter yes to go back to the Main Menu or any other key to cancel");
keepgoing = Console.ReadLine();


} while (keepgoing.Equals("yes"));
Console.ReadLine();
}

}
}
Answers (4)