此处代码._。
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”); ; ; 有人能帮我吗?提前谢谢!
答案 0 :(得分:4)
以下代码不会关闭阅读器 - 它会再次关闭编写器 。即使它有效,它也会在您真正阅读之前关闭阅读器。
System.IO.StreamReader fileUsername = new System.IO.StreamReader("C:\\Users\\Public\\Usernames.txt");
file.Close();
string retrievedUsername = fileUsername.ReadToEnd();
file.Close();
你应该让你的代码看起来像这样:
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();
}
以下代码也存在缺陷:
System.IO.StreamWriter files = new System.IO.StreamWriter("C:\\Users\\Public\\Usernames.txt");
file.WriteLine(registeredUsername);
file.Close();
看到错误?您正在打开(而不是关闭!!)名为StreamWriter
的{{1}},但您正在尝试撰写并尝试关闭files
。大错。
这也可以这样解决:
file
答案 1 :(得分:0)
您永远不会关闭StreamReader
对象fileUsername
。阅读完文件后,您可以调用fileUsername.Close()
,但最好使用using
语句。
using (StreamReader fileUserName = new StreamReader("C:\\Users\\Public\\Usernames.txt"))
{
}
这样,对象在完成操作后就可以正常关闭。
答案 2 :(得分:0)
您尚未关闭名为StreamReader
fileUsername
对象
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();
fileUsername.Close();// <--- This line
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");}
}
}
答案 3 :(得分:0)
您错过了fileUsername StreamReader
的关闭,但我会更改以下代码
string loginUsername = Console.ReadLine();
string readText = File.ReadAllText(path);
if(readText==loginUsername)
{
Console.WriteLine("Welcome back user!");
}else if(loginUsername == "register")
{
Console.WriteLine("Choose your username!");
string registeredUsername = Console.ReadLine();
File.WriteAllText(path,registeredUsername);
}