错误:帮助 D:System.IO.IOException:进程无法访问该文件,因为它正被另一个进程使用 [英] Error: Help D: System.IO.IOException: The process cannot access the file because it is being used by another process
问题描述
代码在这里._.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Public\\Usernames.txt");
file.WriteLine();
file.Close();
int userType = 0;
System.IO.StreamReader fileUsername =
new System.IO.StreamReader("C:\\Users\\Public\\Usernames.txt");
file.Close();
string retrievedUsername = fileUsername.ReadToEnd();
file.Close();
Console.WriteLine("Please note that this is a prototype, passwords are not hashed/encrypted ^_^");
Console.WriteLine("Welcome to the meData service! Ver. 0.01 Beta, made by mechron");
Console.WriteLine("Please enter your username below or type register to register a new account on this device");
string loginUsername = Console.ReadLine();
if (loginUsername == retrievedUsername)
{
Console.WriteLine("Welcome back user!");
userType = 1;
}
else
{
if (loginUsername == "register")
{
Console.WriteLine("Choose your username!");
string registeredUsername = Console.ReadLine();
System.IO.StreamWriter files = new System.IO.StreamWriter("C:\\Users\\Public\\Usernames.txt");
file.WriteLine(registeredUsername);
file.Close();
}
else
{
Console.WriteLine("Error, command not recognized");}
}
}
}
}
我上面的代码^^ Erm...好吧...我遇到了这个问题... System.IO.IOException:进程无法访问该文件,因为它正被另一个进程使用当我点击这一行时不断弹出 System.IO.StreamWriter files = new System.IO.StreamWriter("C:\Users\Public\Usernames.txt");;;有人能帮我吗?提前致谢!
My code above ^^ Erm... well... I'm having trouble with this.... System.IO.IOException: The process cannot access the file because it is being used by another process keeps popping up when I hit this line System.IO.StreamWriter files = new System.IO.StreamWriter("C:\Users\Public\Usernames.txt"); ;; Can someone help me? Thanks in advance!
推荐答案
以下代码不会关闭读取器 - 它会再次关闭写入器.即使它有效,它也会在您真正阅读之前关闭阅读器.
The following code does not close the reader - it closes the writer again. Even if it worked it would close the reader before you actually read.
System.IO.StreamReader fileUsername = new System.IO.StreamReader("C:\\Users\\Public\\Usernames.txt");
file.Close();
string retrievedUsername = fileUsername.ReadToEnd();
file.Close();
你应该让你的代码看起来像这样:
You should make your code look like this:
using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Public\\Usernames.txt"))
{
file.WriteLine();
}
int userType = 0;
string retrievedUsername = String.Empty;
using (System.IO.StreamReader fileUsername = new System.IO.StreamReader("C:\\Users\\Public\\Usernames.txt"))
{
retrievedUsername = fileUsername.ReadToEnd();
}
<小时>
下面的代码也有缺陷:
The code that follows is also flawed:
System.IO.StreamWriter files = new System.IO.StreamWriter("C:\\Users\\Public\\Usernames.txt");
file.WriteLine(registeredUsername);
file.Close();
看到错误了吗?您正在打开(而不是关闭!!)一个名为 files
的 StreamWriter
,但您正在尝试编写并尝试关闭 file
.大错.
See the error? You're opening (and not closing!!) a StreamWriter
called files
, but you're trying to write and trying to close file
. Big mistake.
这也可以像这样修复:
using (System.IO.StreamWriter files = new System.IO.StreamWriter("C:\\Users\\Public\\Usernames.txt"))
{
files.WriteLine(registeredUsername);
}
这篇关于错误:帮助 D:System.IO.IOException:进程无法访问该文件,因为它正被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!