0

I have this text in a .txt file

Username:Password
Username2:Password2
Username3:Password3

And i want to set the value of line 2(example) inside the .txt file to the textboxes

This is what i mean:

Textbox1.text = Username2;
Textbox2.text = Password2;

Any links that could provide help are really appreciated, thanks in advance

Mike Aguilar
  • 348
  • 2
  • 3
  • 14
n0degg
  • 35
  • 7
  • There are some steps you need to do: read the file, split on line breaks, split on `:`, (put the results in a dictionary,) and display the results in a textbox. - Are you having specific problems with any of these steps? – Caramiriel May 02 '18 at 17:53
  • I really don't know how to do all of that, if you have any links that could help me i would appreciate it – n0degg May 02 '18 at 17:58
  • Have a look a the post below, might just get you started https://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line – Pieter Alberts May 02 '18 at 18:02

1 Answers1

0

You can do like this :

// Reading all lines of files
var txtLines =  File.ReadAllLines("your_file_path");

// Make sure, there should be atleast more than one line
// as you want to get username/password from second line
if(txtLines != null && txtLines.Count() > 1)
{
    // Skip first line and after that get first line 
    var secondLine = txtLines.ToList().Skip(1).First();

    // split it by colon
    var splittedText = secondLine.Split(':');
    if(splittedText.Count() > 1)
    {
        Textbox1.Text = splittedText[0];
        Textbox2.Text = splittedText[1];
    }
}
Akash KC
  • 16,057
  • 6
  • 39
  • 59
  • Thanks for the quick reply, if i want to get line 3 as well for 2 more textboxes do i have to change the code like this `var thirdLine = txtLines.ToList().Skip(2).First();` `var splittedText = thirdLine.Split(':');` – n0degg May 02 '18 at 18:05
  • 1
    yeah, you can do in that way too. But I recommend to get necessary item list once so it would be one fetch from list instead of two fetch. – Akash KC May 02 '18 at 18:07
  • @downvoter : Care to comment so that I can improve my answer. – Akash KC May 02 '18 at 23:30