软件编程
位置:首页>> 软件编程>> C#编程>> C#读取txt文件数据的方法实例

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

作者:Hero_rong  发布时间:2022-12-08 15:25:41 

标签:c#,txt,数据

第一步新建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文件数据的方法实例

来源:https://blog.csdn.net/Hero_rong/article/details/116661140

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com