C#从txt文件读取数据

灰太狼 2023-01-20 06:55 52阅读 0赞

目录

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

第二步读取数据

最终效果图


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

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

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0hlcm9fcm9uZw_size_16_color_FFFFFF_t_70

第二步读取数据

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

注意

  • 这里的Phone_Load事件一定要加载上,如果没加载,不会报错,也不会出效果
  • 如果文件里有中文的话,可能会乱码,可以看情况把 GB2312 换成 UTF-8
  1. private void Phone_Load(object sender, EventArgs e)
  2. {
  3. string ReadLine;
  4. string[] array;
  5. string Path = @"D:\FrontierApp.TXT";
  6. StreamReader reader = new StreamReader(Path,System.Text.Encoding.GetEncoding("GB2312"));
  7. while (reader.Peek() >= 0)
  8. {
  9. try
  10. {
  11. ReadLine = reader.ReadLine();
  12. if (ReadLine != "")
  13. {
  14. ReadLine = ReadLine.Replace("\"", "");
  15. array = ReadLine.Split(',');
  16. if (array.Length == 0)
  17. {
  18. MessageBox.Show("您选择的导入数据类型有误,请重试!");
  19. return;
  20. }
  21. for (int i = 0; i < array.Length; i++)
  22. {
  23. comboBox2.Items.Add(array[i]);
  24. }
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. MessageBox.Show(ex.ToString());
  30. }
  31. }
  32. }

最终效果图

20210511165330184.png

C# 萌新真的太不容易了,嘤嘤 ╥﹏╥…

发表评论

表情:
评论列表 (有 0 条评论,52人围观)

还没有评论,来说两句吧...

相关阅读