C#中StreamReader已定义关闭仍提示文件被其他进程占用问题求助
问题描述
我是C#新手,对OOP(面向对象编程)和低耦合技术不熟悉。目前遇到问题:我认为已在方法内关闭StreamReader,但仍提示文件被其他进程占用。该StreamReader的Read方法位于ReadandWriteTxtfile类中。
相关核心代码
1. MMCM Library_main
public partial class MMCMLibrary_home : Form { // 实例化背景颜色类 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; ReadandWriteTxtfile Writer = new ReadandWriteTxtfile(); ReadandWriteTxtfile Reader = new ReadandWriteTxtfile(); public MMCMLibrary_reserve(int roomNumber) { InitializeComponent(); room = roomNumber; instance = this; } public void btnDCRoomsReserve_Click(object sender, EventArgs e) { if (rbtn9.Checked) { if (room == 1) { if (Reader.Read("DCR1_9am.txt") == "1") MessageBox.Show("该时段已被占用或已过期"); else Writer.Write("DCR1_9am.txt", "1"); } else if (room == 2) { if (Reader.Read("DCR2_9am.txt") == "1") MessageBox.Show("该时段已被占用或已过期"); else Writer.Write("DCR2_9am.txt", "1"); } else if (room == 3) { if (Reader.Read("DCR2_9am.txt") == "1") MessageBox.Show("该时段已被占用或已过期"); else Writer.Write("DCR3_9am.txt", "1"); } else if (room == 4) { if (Reader.Read("DCR4_9am.txt") == "1") MessageBox.Show("该时段已被占用或已过期"); 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; } } } }
报错截图

解决方案
问题根源
你的Read方法里,Read.Close()永远不会执行——因为return Read.ReadLine();语句会直接终止方法,后续代码根本无法运行。这就导致StreamReader一直处于打开状态,文件被进程占用。另外,就算把Close移到return前,若读取过程抛出异常,Close同样不会执行,仍会导致文件占用。
修复步骤
- 用
using语句自动释放资源:C#中StreamReader和StreamWriter都实现了IDisposable接口,用using包裹后,代码块结束时会自动调用Dispose方法,关闭流并释放资源,就算出现异常也能保证资源正常释放。
修改后的ReadandWriteTxtfile类:
internal class ReadandWriteTxtfile { // 抽离路径拼接逻辑,避免重复代码 private string GetFullPath(string filepath) { return Path.Combine(Application.StartupPath, "Database", "DCRTimeFrameColors", filepath); } public void Write(string filepath, string process) { string fullPath = GetFullPath(filepath); using (StreamWriter writer = new StreamWriter(fullPath)) { writer.WriteLine(process); } // 自动关闭并释放writer } public string Read(string filepath) { string fullPath = GetFullPath(filepath); using (StreamReader reader = new StreamReader(fullPath)) { return reader.ReadLine(); } // 自动关闭并释放reader } }
- 额外优化点:
- 删除
MMCMLibrary_home中重复的frmMainScreen_Load方法,避免编译错误; - 修正
MMCMLibrary_reserve中room==3时的读取文件错误(当前读的是DCR2_9am.txt,应改为DCR3_9am.txt)。
- 删除
内容的提问来源于stack exchange,提问作者Javanese Python
相关产品推荐
相关产品推荐

