第一步新建txt文件,写入内容

我是放在D盘下的,数据以逗号隔开的,是英文逗号

C#读取txt文件数据的方法实例

第二步读取数据

在需要读取数据的页面,添加代码,就可以了

 private void Phone_Load(object sender, EventArgs e)
        {
            string ReadLine;
            string[] array;
            string Path = @"D:\FrontierApp.TXT";
 
            StreamReader reader = new StreamReader(Path,System.Text.Encoding.GetEncoding("GB2312"));
            while (reader.Peek() >= 0)
            {
                try
                {
                    ReadLine = reader.ReadLine();
                    if (ReadLine != "")
                    {
                        ReadLine = ReadLine.Replace("\"", "");
                        array = ReadLine.Split(',');
                        if (array.Length == 0)
                        {
                            MessageBox.Show("您选择的导入数据类型有误,请重试!");
                            return;
                        }
                        for (int i = 0; i < array.Length; i++)
                        {
                            comboBox2.Items.Add(array[i]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

最终效果图

C#读取txt文件数据的方法实例

更多关于C#读写txt文件的内容可以参考这篇文章:https://www.jb51.net/article/212136.htm

总结

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。