Search code examples
c#system

Error: Help D: System.IO.IOException: The process cannot access the file because it is being used by another process


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!


Solution

  • 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();
    

    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.

    This can also be fixed like this:

    using (System.IO.StreamWriter files = new System.IO.StreamWriter("C:\\Users\\Public\\Usernames.txt"))
    {
        files.WriteLine(registeredUsername);
    }