使用.txt文件

时间:2018-05-02 17:49:11

标签: c# file-read

我在.txt文件中有这个文本

Username:Password
Username2:Password2
Username3:Password3

我想将.txt文件中的第2行(示例)的值设置为文本框

这就是我的意思:

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

任何可提供帮助的链接都非常感谢,提前感谢

1 个答案:

答案 0 :(得分:0)

你可以这样做:

// 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];
    }
}