首页 > 编程语言 > C#读取txt文件数据的方法实例
2021
07-01

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

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

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

第二步读取数据

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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文件的内容可以参考这篇文章:https://www.jb51.net/article/212136.htm

总结

到此这篇关于C#读取txt文件数据的文章就介绍到这了,更多相关C#读取txt文件数据内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧