Code here ._.
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");}
}
}
}
}
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!
you missed the closing of fileUsername
StreamReader
, but I would change the code as belowThe 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.
You should make your code look like this:
The code that follows is also flawed:
See the error? You're opening (and not closing!!) a
StreamWriter
calledfiles
, but you're trying to write and trying to closefile
. Big mistake.This can also be fixed like this:
You haven't closed
StreamReader
object calledfileUsername
You never close the
StreamReader
objectfileUsername
. You can callfileUsername.Close()
after you have finished reading the file, however it would be better to use ausing
statement.That way the object is closed properly after it has finished its operations.