【发布时间】:2023-09-07 22:43:01
【问题描述】:
我收到一个错误,即我用于创建和保存我的字典的文本文件正被另一个进程使用,我使用进程资源管理器没有任何结果可以使用我的文件。以下是我的代码和引发此错误的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace meade_9_10
{
public partial class Form1 : Form
{
private Dictionary<string, string> names = new Dictionary<string, string>()
{
};
public Form1()
{
//Make sure Form1 is loaded and ran on program open
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
//Grab myfile.txt and convert to array
StreamReader sr = new StreamReader("myfile.txt");
string line;
while ((line = sr.ReadLine()) != null)
{
string[] arr = line.Split(',');
int i = 0;
//Add array objects to names dictionary
while (i < arr.Length)
{
names[arr[i]] = arr[i + 1];
i += 2;
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
//declare variables
string nameAdd;
string emailAdd;
//Put user input into variable
nameAdd = txtNameAdd.Text;
emailAdd = txtEmailAdd.Text;
//Declare new dictionary key pair as user input
names[emailAdd] = nameAdd;
//clear the textbox controls
txtNameAdd.Text = "";
txtEmailAdd.Text = "";
}
private void btnDelete_Click(object sender, EventArgs e)
{
string emailDel;
emailDel = txtEmailDel.Text;
//Remove key pair that is inputted by user
names.Remove(emailDel);
}
private void btnChange_Click(object sender, EventArgs e)
{
//Declare variables
string emailDel;
string nameAdd;
string emailAdd;
//Assign values to variables
emailDel = txtEmailChange.Text;
nameAdd = txtNameNew.Text;
emailAdd = txtEmailNew.Text;
//Delete the user inputted email to change
names.Remove(emailDel);
//Add the new key pair values to dictionary
names.Add(emailAdd, nameAdd);
}
private void btnLookUp_Click(object sender, EventArgs e)
{
//Declare variable
string email = txtEmail.Text;
//If statement to check if dictionary contains key value
if (names.ContainsKey(email))
{
outputName.Text = names[email];
outputEmail.Text = email;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
//writes the names dictioanry to array inside text file
File.WriteAllLines("myfile.txt",
names.Select(x => x.Key + "," + x.Value ).ToArray());
//Closes the program
this.Close();
}
}
}
我的代码部分给了我错误
System.IO.IOException: '进程无法访问文件'C:\Users\Adrian\Desktop\ALL SCHOOL FILES\Fall 2021\C#\meade_9_10\bin\Debug\myfile.txt',因为它正在被使用由另一个进程。'
是
names.Select(x => x.Key + "," + x.Value ).ToArray());
我只是不知道是什么进程正在使用我的文本文件破坏了这个程序,它之前工作过,除了删除函数之间的冗余空白之外,我没有做任何更改。
【问题讨论】:
-
读取文件时,之后不要关闭
StreamReader。最简单的解决方法是使用File.ReadAllLines来简化 IO。 -
它告诉你文件正在使用中。它是否在另一个程序中打开?甚至可能有旧版本的应用在后台运行。
-
我已将 File.WriteAllLines 更改为 File.ReadAllLines,但没有任何改变。我已经搜索了我的进程,但是当我运行它时,除了程序之外,找不到可以打开该文件的任何地方。
-
这是因为 StreamReader sr = new StreamReader("myfile.txt");你在加载中做到了这一点,它永远不会关闭。应该使用 using(StreamReader sr = new StreamReader("myfile.txt")){ ..... 其他代码 }
标签: c# arrays dictionary streamwriter