How to check if username already exist in text file and write it if username available c# console application : using System;
using System.IO;

namespace LumberJackProgram
{
    class CreateNewUser
    {
        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");

>  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.'

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

                    }
                }

            }
           
        }
    }
}