I'm new to C# and am unfamiliar with OOP and low coupling techniques. I faced a wall where I believe that I really closed the stream reader before using it. The stream reader method is in the ReadandWriteTxtfile. My classes that utilized StreamReader and StreamWriter: (I'll only show the significant codes) 1. MMCM Library_main public partial class MMCMLibrary_home : Form
{
//Instantiate the Background Colors
TimeFrame_Colors TimeFrame = new TimeFrame_Colors();
}
private void frmMainScreen_Load(object sender, EventArgs e)
{
timerlblClock.Start();
TimeFrame.Colors();
}
private void frmMainScreen_Load(object sender, EventArgs e)
{
timerlblClock.Start();
TimeFrame.Colors();
}
2. ReadandWriteTxtfile using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace MMCM_Library
{
internal class ReadandWriteTxtfile
{
public void Write(string filepath, string process)
{
StreamWriter Write = new StreamWriter(Application.StartupPath + "\\Database\\" + "\\DCRTimeFrameColors\\" + filepath);
Write.WriteLine(process);
Write.Close();
}
public string Read(string filepath)
{
StreamReader Read = new StreamReader(Application.StartupPath + "\\Database\\" + "\\DCRTimeFrameColors\\" + filepath);
return Read.ReadLine();
Read.Close();
}
}
}
3. MMCMLibrary_reserve
namespace MMCM_Library
{
public partial class MMCMLibrary_reserve : Form
{
public static MMCMLibrary_reserve instance;
int room;
//ReadandWrite read = new ReadandWrite();
//ReadandWrite write = new ReadandWrite();
ReadandWriteTxtfile Writer = new ReadandWriteTxtfile();
ReadandWriteTxtfile Reader = new ReadandWriteTxtfile();
//TimeFrame_Colors ColorTimeFrame = new TimeFrame_Colors();
//OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\NITRO 5\Downloads\MMCM LIbrary.accdb");
// OleDbCommand cm = new OleDbCommand();
//OleDbDataReader dr;
//StreamWriter Write = new StreamWriter(Application.StartupPath + "\\Database\\" + "Reservationdata.txt");
public MMCMLibrary_reserve(int roomNumber)
{
InitializeComponent();
room = roomNumber;
instance = this;
}
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
//MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
//All 9am to log if it's reserved (1 = reserved)
if (rbtn9.Checked)
{
if (room == 1)
{
if (Reader.Read("DCR1_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");
else Writer.Write("DCR1_9am.txt", "1");
}
else if (room == 2)
{
if (Reader.Read("DCR2_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");
else Writer.Write("DCR2_9am.txt", "1");
}
else if (room == 3)
{
if (Reader.Read("DCR2_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");
else Writer.Write("DCR3_9am.txt", "1");
}
else if (room == 4)
{
if (Reader.Read("DCR4_9am.txt") == "1") MessageBox.Show("It is already taken or it is already past that time");
else Writer.Write("DCR4_9am.txt", "1");
}
}
4. TimeFrame_Colors namespace MMCM_Library
{
internal class TimeFrame_Colors
{
public static TimeFrame_Colors instance;
ReadandWriteTxtfile Reader = new ReadandWriteTxtfile();
public void Colors()
{
instance = this;
//DCR 1
if (Reader.Read("DCR1_9am.txt") == "1")
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
if (Reader.Read("DCR1_11am.txt") == "1")
{
MMCMLibrary_home.instance.lbl1_2.BackColor = System.Drawing.Color.Red;
}
if (Reader.Read("DCR1_1pm.txt") == "1")
{
MMCMLibrary_home.instance.lbl1_3.BackColor = System.Drawing.Color.Red;
}
if (Reader.Read("DCR1_3pm.txt") == "1")
{
MMCMLibrary_home.instance.lbl1_4.BackColor = System.Drawing.Color.Red;
}
What is StreamReader used for?
StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file. This type implements the IDisposable interface.
How do I use StreamReader in C sharp?
C# StreamReader example to read one line. using System;. using System.IO;. public class StreamReaderExample.. public static void Main(string[] args). FileStream f = new FileStream("e:\\output.txt", FileMode.OpenOrCreate);. StreamReader s = new StreamReader(f);.
How do I read a line from a text file in C#?
The ReadLine method of StreamReader reads one line at a time.. // Read file using StreamReader. Reads file line by line.. using(StreamReader file = new StreamReader(textFile)) {. int counter = 0;. string ln;. while ((ln = file.ReadLine()) != null) {. Console.WriteLine(ln);. counter++;.
What is StreamReader in Visual Basic?
StreamReader handles text files. We read a text file in VB.NET by Using StreamReader. This will correctly dispose of system resources and make code simpler. The best way to use StreamReader requires some special syntax.
|