CrimzonWeb
CrimzonWeb

Reputation: 171

C# Visual Studio - Implementing user authentication using text files

Using visual studio 2013 and Console Application.

My question, How to I make something like this: I have a programm. I want the programm to check if there is a file called Usernames.txt, If there is,do nothing and if there is not, Create a new one.

Now when that Is done, I want the console to ask the person to enter a username, When the person does it, I want the programm to check if the file called: Usernames.txt contains that same username that the person entered, If yes, Then tell the person: This username already exists, Pick a new one. If not then add it to the file and continue on to ask for a password and so on.

This is what I got so far and I don't know what to do next:

            Console.WriteLine("Username: ");
            string uCreation = Console.ReadLine();
            bool exists = false;
            foreach (string lines in File.ReadAllLines("Usernames.txt"))
            {
                if (lines == uCreation)
                {
                    Console.WriteLine("Username already exists!");
                    exists = true;
                    break;
                }
            }
            if (!exists)
            {
                File.AppendAllText(@"Usernames.txt", uCreation + Environment.NewLine);
            }

Am I on the right track? I have no idea D= If anybody could help out with some solutions that would be sweet! Thanks!

Upvotes: 0

Views: 655

Answers (2)

Koder101
Koder101

Reputation: 892

Here is your solution :

static void Main()
    {
        string path = @"C:\Usernames.txt";
        if (File.Exists(path))
            Console.WriteLine("File already exists. Exiting the application...");
        else
        {
            List<string> list = new List<string>();
            string IsContinue = "Y";
            while (IsContinue.Equals("Y"))
            {
                Console.WriteLine("Enter the username --");
                string userName = Console.ReadLine();
                if (!list.Contains(userName))
                {
                    list.Add(userName);
                    File.AppendAllText(path, userName + Environment.NewLine);

                    Console.WriteLine("Success...! Continue (Y/N) ?", userName);
                    IsContinue = Console.ReadLine().ToUpper();
                }
                else
                    Console.WriteLine("{0} already exists. Choose a new Username again.\n", userName);
            }
        }
    }

.................................... enter image description here

Upvotes: 0

nobody
nobody

Reputation: 11090

You should be able to modify this code to suit your requirements.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            if (File.Exists(@"c:\Usernames.txt"))
            {
                Console.WriteLine("File already exists.I am doing nothing.Tadaaaaaaaaaa !!!");
                return;
            }
            else
            {
                string sContinue = "yes";
                HashSet<string> sUserNames = new HashSet<string>();
                while (sContinue.Equals("yes"))
                {
                    Console.WriteLine("Enter username:");
                    string sUserName = Console.ReadLine();
                    if (!sUserNames.Contains(sUserName))
                    {
                        sUserNames.Add(sUserName);
                        using (StreamWriter oWriter = new StreamWriter(@"c:\Usernames.txt", true))
                            oWriter.WriteLine(sUserName);

                        Console.WriteLine("Username {0} was added.Enter yes to continue or no to exit", sUserName);
                    }
                    else
                        Console.WriteLine("Username {0} exists.Enter yes to add new username or no to exit", sUserName);
                    sContinue = Console.ReadLine();
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.WriteLine("Done");
        Console.Read();
    }
}

Output

Usernames.txt

Upvotes: 1

Related Questions