C# System.IO异常“;该进程无法访问该文件,因为另一进程正在使用该文件;
执行控制台应用程序,其中用户输入与文本文件中的数字匹配的“帐号”。找到号码后,显示信息,然后显示一行“如果要删除此帐户”。当用户按下“y”键时,应删除文本文件的内容或删除文本文件本身 到目前为止,我一直收到这样的错误消息:“IOException:进程无法访问文件‘file path’,因为它正被另一个进程使用” 我一直在这行代码上收到这个错误消息:File.Delete(textFile) 下面是我的代码:*我多次尝试编写file.Close(),以确保不再使用它C# System.IO异常“;该进程无法访问该文件,因为另一进程正在使用该文件;,c#,C#,执行控制台应用程序,其中用户输入与文本文件中的数字匹配的“帐号”。找到号码后,显示信息,然后显示一行“如果要删除此帐户”。当用户按下“y”键时,应删除文本文件的内容或删除文本文件本身 到目前为止,我一直收到这样的错误消息:“IOException:进程无法访问文件‘file path’,因为它正被另一个进程使用” 我一直在这行代码上收到这个错误消息:File.Delete(textFile) 下面是我的代码:*我多次尝试编写file.Close(),以确保不再使用它 Consol
Console.WriteLine("Account Number:");
Console.SetCursorPosition(25, 9);
userInput = Console.ReadLine();
StreamReader reading = File.OpenText("account_number.txt");
string str;
while ((str = reading.ReadLine()) != null)
{
if (str.Contains(userInput))
{
Console.SetCursorPosition(10, 19);
Console.WriteLine("Account found!");
Console.SetCursorPosition(17, 10);
using (StreamReader file = new StreamReader(textFile))
{
while ((userInput = file.ReadLine()) != null)
{
if (counter == 6)
{
file.Close();
break;
}
BorderTableTwo();
Console.SetCursorPosition(31, 26);
Console.WriteLine("ACCOUNT DETAILS");
Console.SetCursorPosition(10, y);
Console.WriteLine(userInput);
counter++;
y++;
}
file.Close();
Console.SetCursorPosition(10, 19);
Console.WriteLine("Delete this acc? (y/n):");
Console.SetCursorPosition(60, 22);
userInput2 = Console.ReadLine();
if (userInput2 == "y")
{
File.Delete(textFile);
Console.SetCursorPosition(10, 22);
Console.WriteLine("Account has been deleted now");
DeleteAccount();
}
else if (userInput2 == "n")
{
Console.Clear();
mainMenu();
}
}
}
else
{
Console.SetCursorPosition(10, 16);
Console.WriteLine("Account not found! Try again");
Console.ReadKey();
SearchAccount();
}
}
CheckEsc();
您的
StreamReader
仍处于打开状态。或者至少是其中一个。你完全正确。我现在觉得自己很笨。在我忘记关闭代码之前,我有多个StreamReader