Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Challenge1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Welcome! Please Login");
- Login();
- Console.ReadKey();
- }
- static void Login()
- {
- Console.WriteLine("Username:");
- string username = Console.ReadLine();
- Console.WriteLine("Password:");
- string password = Console.ReadLine();
- if (username == "admin" && password == "123")
- {
- Console.WriteLine("Welcome back! " + username);
- Console.WriteLine("Check the available list of commands by typing 'commands'");
- string input = Console.ReadLine();
- switch (input)
- {
- default:
- Console.WriteLine("Oops! You entered a wrong command! Use 'commands' for help");
- input = Console.ReadLine();
- break;
- case "commands":
- Console.WriteLine("Available commands: adduser | addfile | showusers | showfiles | saveusers | savefiles | loadusers | loadfiles | exit");
- input = Console.ReadLine();
- break;
- case "adduser":
- Addusers();
- break;
- case "addfile":
- Addfiles();
- break;
- case "showusers":
- Showusers();
- break;
- case "showfiles":
- Showfiles();
- break;
- case "deleteusers":
- Deleteusers();
- break;
- case "deletefiles":
- //code
- break;
- case "exit":
- break;
- }
- } else
- {
- Console.WriteLine("You have entered a wrong Username or Password! Please try again");
- Login();
- }
- }
- static void Addusers()
- {
- Console.WriteLine("New Username:");
- string username = Console.ReadLine();
- Console.WriteLine("New Password for user " + username);
- string password = Console.ReadLine();
- string userdata = username + ";" + password;
- FileStream userfile = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
- TextWriter writer = new StreamWriter(userfile);
- writer.WriteLine(userdata);
- writer.Flush();
- writer.Close();
- Console.WriteLine("User with Username: " + username + " and Password: " + password + " has been added Successfully!");
- }
- static void Addfiles()
- {
- Console.WriteLine("New FIle Name:");
- string filename = Console.ReadLine();
- Console.WriteLine("Contents of the new file named: " + filename);
- string contents = Console.ReadLine();
- string userdata = filename + ";" + contents;
- FileStream userfile = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "/files.txt");
- TextWriter writer = new StreamWriter(userfile);
- writer.WriteLine(userdata);
- writer.Flush();
- writer.Close();
- Console.WriteLine("File with Name: " + filename + " and Contents: " + contents + " has been added Successfully!");
- }
- static void Showusers() // Don't know how to make it loop not just read 1st line
- {
- Console.WriteLine("Opening users.txt Please wait...");
- Console.WriteLine("Users in Database:");
- FileStream userfile = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
- TextReader reader = new StreamReader(userfile);
- string userdata = reader.ReadLine();
- reader.Close();
- Console.WriteLine(userdata);
- }
- static void Showfiles() // Don't know how to make it loop not just read 1st line
- {
- Console.WriteLine("Opening files.txt Please wait...");
- Console.WriteLine("Files in Database:");
- FileStream userfile = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "/files.txt");
- TextReader reader = new StreamReader(userfile);
- string userdata = reader.ReadLine();
- reader.Close();
- Console.WriteLine(userdata);
- }
- // Not working bellow!
- static void Deleteusers()
- {
- Console.WriteLine("The following users are present in the file: ");
- Showusers();
- Console.WriteLine("Enter the Username to be deleted:");
- string username = Console.ReadLine();
- FileStream userfile = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
- TextWriter writer = new StreamWriter(userfile);
- writer.Close();
- // Missing code //
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement