Error: Help D: System.IO.IOException: The process

2019-07-23 13:54发布

问题:

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!

回答1:

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);
}


回答2:

You never close the StreamReader object fileUsername. You can call fileUsername.Close() after you have finished reading the file, however it would be better to use a using statement.

 using (StreamReader fileUserName = new StreamReader("C:\\Users\\Public\\Usernames.txt")) 
 {

 }

That way the object is closed properly after it has finished its operations.



回答3:

You haven't closed StreamReader object called 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");}

      }


   }


回答4:

you missed the closing of fileUsername StreamReader , but I would change the code as below

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);
}


标签: c# system