c# - How to check where in your text file you found a string -
edit: fixed :) thanks!
i'm trying make login code , i'm having bit of trouble @ login function. need check line username located in, within text file. possible? if not there way around this?
edit: added code
using system; using system.collections.generic; using system.linq; using system.text; namespace consoleapplication1 { class program { static void main(string[] args) { int loginattempt = 0; using (system.io.streamwriter file = new system.io.streamwriter("c:\\users\\public\\usernames.txt", true)) { file.writeline(); } int usertype = 0; string retrievedusername = string.empty; using (system.io.streamreader fileusername = new system.io.streamreader("c:\\users\\public\\usernames.txt")) { retrievedusername = fileusername.readtoend(); } console.foregroundcolor = consolecolor.magenta; console.writeline("please note prototype, passwords not hashed/encrypted ^_^. report bugs / flaws andrew.yu6@hotmail.com, if spam high 5 in face chair"); console.foregroundcolor = consolecolor.green; console.writeline("welcome medata service! ver. 0.01 beta, made mechron"); { console.writeline("please enter login below or type register register new account on device"); string loginusername = console.readline(); if (loginusername == "login") { console.writeline("type in username"); string loginuser = console.readline(); } else { if (loginusername == "register") { int registerattempt = 0; console.writeline("choose username!"); string registerusername = console.readline(); bool containuser = system.io.file.readalllines("c:\\users\\public\\usernames.txt").contains(registerusername); if (containuser == true) { console.writeline("error, username exists! please try again!"); } else { registerattempt = 1; } if( registerattempt == 1 ) { using (system.io.streamwriter files = new system.io.streamwriter("c:\\users\\public\\usernames.txt", true)) { files.writeline(registerusername); } } if (registerattempt == 1) { console.writeline("now choose password!"); string loginpass = console.readline(); using (system.io.streamwriter files = new system.io.streamwriter("c:\\users\\public\\passwords.txt", true)) { files.writeline(loginpass); } console.writeline("good! returned start screen! try login there!"); } } else { console.writeline("error, command not recognized. try again"); } } } while ( loginattempt == 0 );
try this:
if(loginusername == "login") { console.writeline("type in username"); string loginuser = console.readline(); int linecount=1; foreach(var line in file.readlines("loginfile.txt")) { if(line.contains(loginuser)) { console.writeline("username found in line no = "+ linecount); break; } linecount++; } }
Comments
Post a Comment