我是 C# 新手,不熟悉 OOP 和低耦合技術。我面對一堵墻,我相信在使用它之前我真的關閉了流閱讀器。流閱讀器方法在 ReadandWriteTxtfile 中。
我使用 StreamReader 和 StreamWriter 的課程:( 我只會展示重要的代碼)
1.MMCM庫_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.讀寫文本檔案
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;
}

uj5u.com熱心網友回復:
使用檔案流最安全的方法是使用 using 陳述句。
public void Write(string filepath, string process)
{
using (StreamWriter Write = new StreamWriter(Application.StartupPath "\\Database\\" "\\DCRTimeFrameColors\\" filepath))
{
Write.WriteLine(process);
}
}
public string Read(string filepath)
{
using (StreamReader Read = new StreamReader (Application.StartupPath "\\Database\\" "\\DCRTimeFrameColors\\" filepath))
{
return Read.ReadLine();
}
}
我根本不會手動呼叫 Close ,而是使用 using 陳述句......但如果您只是嘗試將文本寫入或讀取檔案,請使用一次性File.WriteAllText和File.ReadAllText呼叫:
File.WriteAllText(string filepath, string process);
File.ReadAllText(string filepath);
uj5u.com熱心網友回復:
StreamWriter 和其他幾個類實作了 IDisposable 介面。IDisposable 通常使用以下語法實作:
using (StreamWriter writer = new StreamWriter(input))
{
// work with writer is done here
}
這里的語法旨在為最終將被垃圾收集的物件創建一個范圍。括號中發生的任何事情都將在作者的一生中有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/513591.html
標籤:C#形式
上一篇:如何防止輸入復選框受空格鍵影響?
