0% found this document useful (0 votes)
185 views6 pages

Hospital Management System Code

The document describes a C# program for a hospital management system that uses file handling. The program allows users to add, delete, update patient records and view hospital information by writing and reading from text files. It contains methods for the menu, adding records, checking if a file exists, reading the file contents, deleting records by searching for and removing matching lines, and updating records by similar searching and replacing logic.

Uploaded by

Zain Rizvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views6 pages

Hospital Management System Code

The document describes a C# program for a hospital management system that uses file handling. The program allows users to add, delete, update patient records and view hospital information by writing and reading from text files. It contains methods for the menu, adding records, checking if a file exists, reading the file contents, deleting records by searching for and removing matching lines, and updating records by similar searching and replacing logic.

Uploaded by

Zain Rizvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Zain Rizvi 02-134162-140 46064

BSE-1(A)

Hospital management System


(File Handling)

Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_2
{
class Program
{
static string filepath = @"C:/Users/szain/Desktop/hms.txt";
static string filepaths = @"C:/Users/szain/Desktop/hmss.txt";
public static void add() {
filestatus();
Console.Write("Name:");
string name = Console.ReadLine();
Console.Write("Gender:");
string gender = Console.ReadLine();
Console.Write("Address:");
string add = Console.ReadLine();
Console.Write("Mobile:");
string mob = Console.ReadLine();
Console.Write("Doctor");
string doctor = Console.ReadLine();
using (StreamWriter writer1 = new StreamWriter(filepath, append: true))
{
writer1.WriteLine(name+","+gender+","+add+","+mob+","+doctor);
writer1.Close();
Console.WriteLine("Record Added \n\n");
}
readfile();
Menu();
}
public static void filestatus() {
if (!File.Exists(filepath))
{
StreamWriter sw = File.CreateText(filepath);
Console.WriteLine("new File Created");
sw.Dispose();
}
else
Console.WriteLine("File Exist");
}
Zain Rizvi 02-134162-140 46064
BSE-1(A)
public static void readfile() {
string content = File.ReadAllText(filepath);
Console.Write(content);
Console.ReadKey();
Menu();
}
public static void Del()
{
Console.Clear();
readfile();
Console.WriteLine("Enter Text to Delete");
string strSearchText = Console.ReadLine();
string strOldText;
string n = "";
StreamReader sr = File.OpenText(filepath);
while ((strOldText = sr.ReadLine()) != null)
{
if (!strOldText.Contains(strSearchText))
{
n += strOldText + Environment.NewLine;
}
}
sr.Close();
File.WriteAllText(filepath, n);
Console.WriteLine("Deleted:");
readfile();
Menu();
}
static void Main(string[] args)
{
Menu();
}
private static void Menu()
{
while (true)
{
Console.WriteLine("Press 1 for Add patient");
Console.WriteLine("Press 2 for Remove patient");
Console.WriteLine("Press 3 for update patient");
Console.WriteLine("Press 4 to view Patient");
Console.WriteLine("Press 5 for View Hospital info");
Console.WriteLine("Press 6 to Exit");
// Console.Write("Press 6 to View Patient file");
int opt = Convert.ToInt32(Console.ReadLine());
if (opt == 1)
{
add();
}
if (opt == 2)
{
Del();
}
if (opt == 3)
{
update();
}
if (opt == 4)
readfile();
Zain Rizvi 02-134162-140 46064
BSE-1(A)
if (opt == 5)
hospital();
else
{
Environment.Exit(0);

}
// readfile();
}
// add();
}

private static void hospital()


{
string content = File.ReadAllText(filepaths);
Console.Write(content);
Console.ReadKey();
Menu();
}
private static void update()
{
Console.Clear();
readfile();
Console.WriteLine("Enter Text to Delete");
string strSearchText = Console.ReadLine();
string strOldText;
string n = "";
StreamReader sr = File.OpenText(filepath);
while ((strOldText = sr.ReadLine()) != null)
{
if (strOldText.Contains(strSearchText))
{
// n += strOldText + Environment.NewLine;
Console.Write("Name:");
string name = Console.ReadLine();
Console.Write("Gender:");
string gender = Console.ReadLine();
Console.Write("Address:");
string add = Console.ReadLine();
Console.Write("Mobile:");
string mob = Console.ReadLine();
Console.Write("Doctor");
string doctor = Console.ReadLine();

n += name + "," + gender + "," + add + "," + mob + "," + doctor +


Environment.NewLine;
}
else
{
n += strOldText + Environment.NewLine;
}
}
sr.Close();
File.WriteAllText(filepath, n);
readfile();

Menu();
}
Zain Rizvi 02-134162-140 46064
BSE-1(A)
}
}

Added New Record

Delete Record

Update
Zain Rizvi 02-134162-140 46064
BSE-1(A)

Hospital information

Filing Screen Shot


Zain Rizvi 02-134162-140 46064
BSE-1(A)

You might also like