Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is a C# assignment. So I have made my code and it works and everything but... now it says that I have to save to a file. Here's my code:

C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Case3Password
{
    internal class Program
    {
        static void Main(string[] args)
        {
            bool que = false;
            while (que == false)
            {
                Console.WriteLine("Choose whether to login, make or change");
                string command = Console.ReadLine();
                if (command == "make" && !MadeAccount)
                {
                    MakeAccount();
                }
                else if (command == "change" && MadeAccount)
                {
                    ChangePassword();
                }
                else if (command == "login" & MadeAccount)
                {
                    Login();
                }
                else
                {
                    Console.WriteLine("incorrect choice, type either 'login', 'make' or 'change'");
                }
            }
        }
        static bool MadeAccount = false;
        static void MakeAccount()
        {
            Console.WriteLine("Input your username");
            string Username = Console.ReadLine();
            Console.WriteLine("Input your password");
            string password = Console.ReadLine();

            if (ContainsUpperAndLower(password) == true && ContainsNumbersAndSpecialCharacters(password) == true && ContainsNoNumbersAtStartOrEnd(password) == true && ContainNoSpace(password) == true && UsernamePasswordTrue(Username, password) == true)
            {
                string folder = @"C:\Temp\";
                string fileName = "Dokumenter.txt";
                string fullPath = fileName;
                string[] lines = { Username, password };
                File.WriteAllLines(fullPath, lines);
                MadeAccount = true;
                Console.WriteLine("Account succesfully created");
            }
            else
            {
                Console.WriteLine("Sorry this password doesn't meet the requirements");
            }
        }
        static void ChangePassword()
        {
            Console.WriteLine("Input your username");
            string username = Console.ReadLine();
            Console.WriteLine("Input your password");
            string password = Console.ReadLine();
            string folder = @"C:\Temp\";
            string fileName = "CSharpCornerAuthors.txt";
            string fullPath = folder + fileName;
            string[] text = File.ReadAllLines(fullPath);
            string PreviousUsername = text[0];
            string PreviousPassword = text[1];

            if (username == PreviousUsername && password == PreviousPassword)
            {
                Console.WriteLine("What do you want to change your password to?");
                string NewPassword = Console.ReadLine();


                if (PreviousPassword != NewPassword)
                {

                    if (ContainsUpperAndLower(password) == true && ContainsNumbersAndSpecialCharacters(password) == true && ContainsNoNumbersAtStartOrEnd(password) == true && ContainNoSpace(password) == true && UsernamePasswordTrue(username, password) == true)
                    {
                        string[] lines = { username, NewPassword };
                        File.WriteAllLines(fullPath, lines);
                        Console.WriteLine("Password succesfully changed");
                    }
                    else
                    {
                        Console.WriteLine("Sorry this password doesn't meet the requirements");
                    }
                }
            }
        }

        static void Login()
        {
            Console.WriteLine("Input your username");
            string username = Console.ReadLine();
            Console.WriteLine("Input your password");
            string password = Console.ReadLine();
            string folder = @"C:\Temp\";
            string fileName = "CSharpCornerAuthors.txt";
            string fullPath = folder + fileName;
            string[] text = File.ReadAllLines(fullPath);
            string PreviousUsername = text[0];
            string PreviousPassword = text[1];
            if (username == PreviousUsername && password == PreviousPassword)
            {
                Console.WriteLine("Succesfully logged in");
            }
            else
            {
                Console.WriteLine("Sorry either your username or password was incorrect");
            }

            //6 metoder stillet op --->
            //Første metode har jeg kaldet LengthCheck og her har jeg lavet en if/else statement
        }
        public static bool LengthCheck(string password) 
        {
            if (password.Length >= 12) //Hvis password er større end/lig med 12 tegn så returnere den en true værdi
            {
                return true;
            }
            else //En else statement hvis nu den password er mindre end 12
            {
                return false; //Hvis den er ´mindre end 12 tegn så kommer den tilbage false
            }

        }

        //2. metode hvor jeg siger at password SKAL anvende både store og små bogstaver
        public static bool ContainsUpperAndLower(string password)
        {
            int i = 0; //det her er tælleren som bliver brugt senere i koden

            //2 booleans som begge er sat til at være false
            bool ContainsUpperCase = false;
            bool ContainsLowerCase = false;

            //Jeg har brugt en while-loop her som siger at password-længde er større end i
            //og UpperCase && LowerCase
            while (i < password.Length && !(ContainsUpperCase && ContainsLowerCase))
            {
                char letter = password[i]; //jeg har sat char letter = password[i]; altså indivduelle bogstaver siden at jeg skal tjekke alle bogstaverne igennem

                if (ContainsUpperCase == false)
                {
                    ContainsUpperCase = Char.IsUpper(letter);
                }
                if (ContainsLowerCase == false)
                {
                    ContainsLowerCase = Char.IsLower(letter);
                }
                i = i + 1; //Man kan også bruge i++ her. Det præcist det samme
            }
            return ContainsUpperCase && ContainsLowerCase;
        }


        public static bool ContainsNumbersAndSpecialCharacters(string password)
        {
            int i = 0;

            bool Numbers = false;
            bool SpecialCharacters = false;

            while (i < password.Length && !(Numbers && SpecialCharacters))
            {
                char letter = password[i];

                if (Numbers == false)
                {
                    Numbers = Char.IsDigit(letter);
                }
                if (SpecialCharacters == false)
                {
                    SpecialCharacters = !Char.IsLetterOrDigit(letter);
                }
                i = i + 1;
            }
            return Numbers && SpecialCharacters;
        }


        public static bool ContainsNoNumbersAtStartOrEnd(string password)
        {
            if (Char.IsDigit(password[0]) == false & Char.IsDigit(password[password.Length - 1]) == false)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        public static bool ContainNoSpace(string password)
        {
            int i = 0;

            bool NoSpace = false;

            while (i < password.Length && !(NoSpace))
            {
                if (NoSpace == false)
                {
                    NoSpace = Char.IsWhiteSpace(password[i]);
                }
                i = i + 1;
            }
            NoSpace = !NoSpace;

            return NoSpace;
        }



        public static bool UsernamePasswordTrue(string password, string Username)
        {
            if (Username.ToLower() != password.ToLower())
            {
                return true;
            }
            else
            {
                return false;
            }

        }

    }
}


What I have tried:

I have tried to look up videos and look up online but I don't really see how I can apply either of what i've seen to my code so i'm hoping someone can help me in here. I've been stuck on this project for so long just because of this whole file thing. I've never really grasped or understood it. Basically I have to save the input from the user when they for example make an account/login/change password etc stuff like that.

Basically I need to Save login and password in clear text in a text file. Which can again be loaded when logging in or when changing the code.
Posted
Updated 13-Dec-22 2:07am
v2

Quote:
Basically I need to Save login and password in clear text in a text file.

Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Secondly, why is it giving you difficulties?
There are loads of ways to store info in a file, ranging from .INI files, .CONFIG files, "straight text" files, through to JSON, XML, CSV, and right up to multiuser databases, and the "right solution" depends on exactly what you have available and what you are going to do with the data. This may explain some of that: That's not a database, dude![^]

But if all you want is a text file, then use one: File.WriteAllText[^] needs only a path to the file, and a string to write, and reading back is just as simple.

So sit down, read the links, and think about exactly what you need to do with your data. The actual implementation is trivial!
 
Share this answer
 
For the login or change password actions you do:
C#
string folder = @"C:\Temp\";
string fileName = "CSharpCornerAuthors.txt";
string fullPath = folder + fileName;
string[] text = File.ReadAllLines(fullPath);

For the create new account you do:
C#
string folder = @"C:\Temp\";
string fileName = "Dokumenter.txt";
string fullPath = fileName;
string[] lines = { Username, password };
File.WriteAllLines(fullPath, lines);

Do you see what is wrong?
 
Share this answer
 
Comments
johnjohn 201 13-Dec-22 7:16am    
hello, I have tried changing all the names to the same so for example every single one to Dokumenter.txt and I've also put only fileName in all of them and when that didnt work i put folder + fileName in all of them. Unless there's another problem you're referring to.
Richard MacCutchan 13-Dec-22 7:44am    
You need to explain what you mean by "when that didnt work". We cannot guess what you have done, or what the results were.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900