How to avoid this `System.IO.IOException`

Dodofold
public static void Main()
{    
    string username = "", password, chkusername;
    bool vald, chk;
    vald = false;
    string choice = "Y";

    while (choice == "Y")
    {                        
        FileStream fs = new FileStream("Users.txt", FileMode.Append, 
            FileAccess.Write, FileShare.Read);
        StreamWriter sw = new StreamWriter(fs);
        Console.WriteLine("Enter Username : ");
        var Createnew = File.ReadAllLines("Users.txt");`
        chkusername = Console.ReadLine();
        foreach(string Dummies in Createnew)
        {
            string[] userline = Dummies.Split('#');

            if(userline[0].Equals(chkusername))
            {
                vald = true;
                Console.Clear();
                Console.WriteLine("username already exists please choose another");
                Console.ReadKey();
                Console.Clear();
                break;
            }

            chk = vald;
            if (vald == true)
            {
                Main();
            }

            while(choice == "Y")
            {
                username = chkusername;
                Console.WriteLine("Enter a Password : ");
                password = Console.ReadLine();

                sw.WriteLine(username + "#" + password);
                sw.Flush();
                sw.Close();
                fs.Close();
                Console.WriteLine("Username Created");
                Console.WriteLine("Do you want to continue ? Y/N");
                choice = Console.ReadLine();
            }
        }           
    }
}

I get an exception here:

 var Createnew = File.ReadAllLines("Users.txt");

System.IO.IOException: 'The process cannot access the file 'C:\Users\admin1\source\repos\LumberJackProgram\LumberJackProgram\Users.txt' because it is being used by another process.'

Steve

First process the Users.txt file storing each name in an HashSet collection of strings

HashSet<string> names = new HashSet<string>();
var lines = File.ReadAllLines("Users.txt");
foreach(string line in lines)
{
    string name = line.Split('#')[0];
    if(!names.Contains(name))
       names.Add(name);
}

Now you can ask your user to enter the information needed and check against the HashSet if the name is already there or not

while (choice == "Y")
{
     Console.WriteLine("Enter Username : ");
     chkusername = Console.ReadLine();
     if(names.Contains(chkusername))
         Console.WriteLine("Name already entered");
     else
     {
        Console.WriteLine("Enter a Password : ");
        password = Console.ReadLine();
        names.Add(chkusername);

        // Write the file in append mode and close/dispose it
        using(StreamWriter sw = new StreamWriter("Users.txt",true))
            sw.WriteLine(chkusername + "#" + password);

     }
     Console.WriteLine("Continue? Y/N");
     choice = Console.ReadLine();
}

In this way you don't mix the reading part with the writing part. Also you don't need to recall the Main method to reenter this code if the name exists in your file

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid this java.io.IOException: No space left on device

From Dev

How to avoid this java.io.IOException: No space left on device

From Dev

ImageResizer diskcache System.IO.IOException

From Java

How to get rid of - "java.io.IOException: The system cannot find the path specified"

From Dev

How can I solve this? "A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll"

From Dev

System.IO.IOException when calling System.Console.WindowWidth

From Dev

System.IO.IOException error, can't get access to the file

From Dev

Rebex sftpClient.Getfile throws System.IO.IOException in JAMS

From Dev

"System.IO.IOException: The operation completed successfully" exception

From Dev

System.IO.IOException C# when FileInfo and WriteAllLines

From Dev

System.IO.IOException: Too many open files

From Dev

System.IO.IOException: -----END RSA PRIVATE KEY not found

From Dev

System.IO.IOException: Too many open files

From Dev

System.IO.IOException: The process cannot access the file

From Dev

VB.net chosing serial port System.IO.IOException

From Dev

System.IO.IOException on HttpClient.GetAsync when user is not logged in

From Dev

System.IO.IOException: 'Process can not access the file'

From Java

System.IO.IOException: "The file exists" when using System.IO.Path.GetTempFileName() - resolutions?

From Dev

How to solve: java.io.IOException: Stream closed

From Dev

How to write a junit test for outputStream.write IO IOException

From Dev

how to avoid that=this on JS using socket.io?

From Dev

Deploy to Azure from Git Local System.IO.IOException: There is not enough space on the disk

From Dev

AuthenticateAsClient: System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream

From Dev

Error "An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll"

From Dev

Creating zip from Log4Net Log files causes System.IO.IOException

From Dev

java.io.IOException: Cannot run program "dir": CreateProcess error=2, Das System

From Dev

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

From Dev

System.IO.IOException: The process cannot access the file '.txt' because it is being used by another process

From Dev

Error “An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll”

Related Related

  1. 1

    How to avoid this java.io.IOException: No space left on device

  2. 2

    How to avoid this java.io.IOException: No space left on device

  3. 3

    ImageResizer diskcache System.IO.IOException

  4. 4

    How to get rid of - "java.io.IOException: The system cannot find the path specified"

  5. 5

    How can I solve this? "A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll"

  6. 6

    System.IO.IOException when calling System.Console.WindowWidth

  7. 7

    System.IO.IOException error, can't get access to the file

  8. 8

    Rebex sftpClient.Getfile throws System.IO.IOException in JAMS

  9. 9

    "System.IO.IOException: The operation completed successfully" exception

  10. 10

    System.IO.IOException C# when FileInfo and WriteAllLines

  11. 11

    System.IO.IOException: Too many open files

  12. 12

    System.IO.IOException: -----END RSA PRIVATE KEY not found

  13. 13

    System.IO.IOException: Too many open files

  14. 14

    System.IO.IOException: The process cannot access the file

  15. 15

    VB.net chosing serial port System.IO.IOException

  16. 16

    System.IO.IOException on HttpClient.GetAsync when user is not logged in

  17. 17

    System.IO.IOException: 'Process can not access the file'

  18. 18

    System.IO.IOException: "The file exists" when using System.IO.Path.GetTempFileName() - resolutions?

  19. 19

    How to solve: java.io.IOException: Stream closed

  20. 20

    How to write a junit test for outputStream.write IO IOException

  21. 21

    how to avoid that=this on JS using socket.io?

  22. 22

    Deploy to Azure from Git Local System.IO.IOException: There is not enough space on the disk

  23. 23

    AuthenticateAsClient: System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream

  24. 24

    Error "An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll"

  25. 25

    Creating zip from Log4Net Log files causes System.IO.IOException

  26. 26

    java.io.IOException: Cannot run program "dir": CreateProcess error=2, Das System

  27. 27

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

  28. 28

    System.IO.IOException: The process cannot access the file '.txt' because it is being used by another process

  29. 29

    Error “An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll”

HotTag

Archive