如何将文本文件的特定文本加载到特定的文本框



我在C#窗口表单应用程序中创建了一个项目。我使用的是.Net框架版本4.0和Visual Studio 2010。 项目包含"保存并加载文件"按钮。还有一些文本框。

我创建了一个这样的文本文件

Serial Number = 1
Type Number = 500
Test Engineer = jay
Date = 03/05/2018
Time = 16:17:20 PM
Test1 = 1.00
Test2 = 1.76
.
.
.
Test18 = 4.66

"加载文件"按钮的代码:

private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('n');
tb_SerialNo.Text = lines[0];
tb_TypeNo.Text = lines[1];
tb_TestEngineer.Text = lines[2];
tb_Date.Text = lines[3];
tb_Test1.Text = lines[4];
tb_Test2.Text = lines[5];
}  
}

当我运行上面的代码时,我在串行中获得了值,没有文本框Serial Number = 1但我想在文本框中1Type NumberTex 框相同Type Number = 500但在这里我也想在类型数字文本框中500

当您按换行符拆分时,lines[0]将存储Serial Number = 1。在这里,您需要再次拆分它=.

如果您尝试打印字符串数组中每个元素的值,您将了解需要在代码中执行哪些更改。

private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('n'); //To make your code more readable, you can use "Environment.NewLine" instead of 'n'
Console.WriteLine(lines[0]); //Here it will give "Serial Number = 1"
// you need to store 1 in tb_SerialNo.Text, so split lines[0] with =
//Have you tried with this.
string[] splitWithEqualTo = lines[0].Split('=');
tb_SerialNo.Text = splitWithEqualTo [1];
//Similar kind of logic you can apply for other text boxes.
}  
}

要解决您的问题,您可以尝试以下操作

Console.WriteLine(lines[0]); // This will print "Serial Number = 1"
string[] slitLine = lines[0].Split('=');
Console.WriteLine(slitLine[0]); //This will print "Serial Number"
Console.WriteLine(slitLine[1]); //This will print 1, this is what you need to store in tb_SerialNo.Text, right?

这不是解决方案,但您将了解需要在代码中进行哪些更改。


private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('n');
PrintText(tb_SerialNo, lines[0]);
PrintText(tb_TypeNo , lines[1]);
PrintText(tb_TestEngineer, lines[2]);
PrintText(tb_Date, lines[3]);
PrintText(tb_Test1, lines[4]);
PrintText(tb_Test2, lines[5]);
}
}
private void PrintText(TextBox control, string line)
{
var splitline = line.Split('=');
control.Text = splitline[1];
}

试试这个

你可以使用字符串。拆分(( 或字符串。LastIndexOf(( 从原始字符串中提取您需要的部分。

例如:

在这里,当找到'='字符时,我们将字符串一分为二。多余的空格由Trim()解除,用于删除字符串的前导和尾随部分的空格。

tb_Test2.Text = lines[5].Split('=').Last().Trim();

LastIndexOf()找到指定的符号,从字符串的末尾开始搜索并返回其位置(如果找到,否则为 -1(。

Substring()从提供的字符串生成一个新字符串,从一个位置开始并采用指定数量的字符。
在这里,从LastIndexOf()返回的索引开始,将所有字符包括到字符串的末尾(如果您不指定它必须占用多少个字符,它将全部使用它们。这是一个方法重载(。

tb_Date.Text = lines[3].Substring(lines[3].LastIndexOf("=") + 1).TrimStart();

在这两种情况下,原始字符串保持不变。

您还可以从原始数组创建一个新数组,仅包含所需的部分,然后分配新数组的值:

string[] lines2 = lines.Select(s => s.Split('=').Last().Trim()).ToArray();
tb_SerialNo.Text = lines2[0];
tb_TypeNo.Text = lines2[1];
//(...)

最新更新