Tuesday 15 March 2011

.net - How to read multilines from text file in c# -



.net - How to read multilines from text file in c# -

i have text file contains user records.in text file 1 user record nowadays in 3 lines of text file.now per requirement have read first 3 lines 1 user, process , insert database , next 3 lines sec user , on..

here code have used single line reading text files..

if (system.io.file.exists(location) == true) { using (streamreader reader = new streamreader(location)) { while ((line = reader.readline()) != null) { line = line.trim(); } } }

please help me read multi-lines , in case 3 lines text file ..

thanks..

you like:

if (system.io.file.exists(location) == true) { var lines=file.readalllines(location); int usersnumber = lines.count() / 3; for(int i=0; < usersnumber; i++){ var firstfield=lines[i*3]; var secondfield=lines[i*3 +1]; var thirdfield=lines[i*3 +2]; dostuffs(firstfield,secondfield,thirdfield); } if(lines.count() > usersnumber *3) //in case there'd spare lines left dosomethingelsefrom(lines, index=(usersnumber*3 +1)); }

you're reading lines of file, counting how many users have (group of 3), each grouping you're retrieving associate info, , in end you're processing grouping of 3 fields related same user.

c# .net text-files streamreader

No comments:

Post a Comment