将文本文件中的特定行显示到文本框中 c# [英] display specific line in text file into textboxes c#

查看:27
本文介绍了将文本文件中的特定行显示到文本框中 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在特定的文本框中显示 ProductData.txt 的部分内容.用户必须首先输入产品密钥来检查产品是否存在,如果存在,将显示产品的部分信息,如名称、类型和品牌.如果产品密钥不存在,它会显示messagebox: item key not found.我的问题是显示的数据总是第一行.如何显示找到产品密钥的行?

I need to display the partial contents of the ProductData.txt in specific textboxes. The user must first input the product key to check if the product exists and if it does, it would display partial information about the product such as its name, type, and brand. If the product key does not exist, it would display messagebox: item key not found. my problem is that the data being displayed is always the first line. how can i display the line where the product key has been found?

代码片段

        string plist = @"ProductData.txt";
        string s_temp = @"stock_temp.txt";

        string[] dataline = File.ReadAllLines(plist);
        if (File.ReadAllText(plist).Contains(txt_pk.Text))
        {
            using (StreamWriter w = File.AppendText(s_temp))
            {
                foreach (var line in dataline)
                {
                    if (line.Contains(txt_pk.Text))
                    {
                        txt_pk.ReadOnly = true;

                        string fileData = File.ReadAllText(plist);
                        string[] parts = fileData.Split(',');

                        txt_pn.Text = parts[2];
                        cmb_brand.Text = parts[3];
                        cmb_type.Text = parts[4];
                    }
                }
                w.Close();
            }
        }
        else
            MessageBox.Show("Item Key not found!");

样本数据

项目关键字、添加日期、产品名称、品牌、类型

ITEM KEY,DATE ADDED,PRODUCT NAME,BRAND,TYPE

0001,10/08/2017,5s,Apple,Phone

0001,10/08/2017,5s,Apple,Phone

0002,10/08/2017,S5,三星,手机

0002,10/08/2017,S5,Samsung,Phone

推荐答案

string[] parts = line.Split(','); 

您需要拆分行,而不是再次读取文本文件.

You need to split the line, not read the text file again.

专业提示:试一试调试代码(按F11)

Pro Tip: give Debugging a go and step through your code (pressing F11)

这篇关于将文本文件中的特定行显示到文本框中 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆