温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# List如何生成Txt文档且读取Txt文档封装List

发布时间:2022-08-10 10:47:40 来源:亿速云 阅读:181 作者:iii 栏目:开发技术

这篇文章主要介绍“C# List如何生成Txt文档且读取Txt文档封装List”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C# List如何生成Txt文档且读取Txt文档封装List”文章能帮助大家解决问题。

一、简单设计界面

做一个简单的界面,两个触发按钮,对我们写的代码,进行控制(可以直接跳到后面看代码)

C# List如何生成Txt文档且读取Txt文档封装List

窗体触发函数,双击按钮得到的触发函数,编写触发函数的触发条件和逻辑

C# List如何生成Txt文档且读取Txt文档封装List

二、方法函数讲解

总共两个函数,一个读,一个写,分别对应不同的代码,代码逻辑很简单,可以直接拷贝使用,也可以根据自己的需求做更改。代码在文末

C# List如何生成Txt文档且读取Txt文档封装List

C# List如何生成Txt文档且读取Txt文档封装List

三、效果展示

博主只是模拟一部风数据,将数据放进list中,再设置好你需要放置文件的位置,你可以设置任意位置,调用函数将list和文件保存路径作为参数传过去就可以了

C# List如何生成Txt文档且读取Txt文档封装List

C# List如何生成Txt文档且读取Txt文档封装List

生成之后的文件如上图所示,Txt文件中有100个数值。接下来我们把这个txt文件中的数据再读出来,封装进List中。

C# List如何生成Txt文档且读取Txt文档封装List

C# List如何生成Txt文档且读取Txt文档封装List

C# List如何生成Txt文档且读取Txt文档封装List

四、代码逻辑

界面代码:

using DictionaryToTxtFile;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IC00520
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static Dao dao = new Dao();//调用方法的声明
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();//定义打开文件类
            openFile.Filter = "Txt文件(*.Txt)|*.txt|所有文件|*.*";
            if (openFile.ShowDialog() != DialogResult.OK)//打开文件是否点击了取消
                return;
            string path = openFile.FileName;//获得你打开文件的名称,路径
            List<int> datalist= dao.WriteData(path);//调用方法
            foreach(int i in datalist)//遍历展示数据
            {
                richTextBox1.Text += i.ToString()+"\r\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<int> list = new List<int>();
            for(int i = 0; i<100;i++)//生成100个虚拟数据测试
            {
                list.Add(i);
            }
            dao.SaveData(list, "C:\Users\ASUS\source\repos\IC00520\IC00520\");//将数据和文件路径作为实参
        }
    }
}

方法类代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryToTxtFile
{
    public class Dao
    {
        public string SaveData(List<int> info,string path)
        {
            try
            {
                string dataName = "";//数据下发文件名
                string Data = "";
                foreach (var item in info)//遍历List数据将数据保存在Data中
                {
                    Data += item + "\r\n";
                }
                dataName = "IC00" + DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss")+ ".txt";//以日期创建文件名
                path += dataName;
                File.AppendAllText(path, Data);//直接追加,如果没有这个文件会自动添加
                return "OK";
            }
           catch(Exception ex)
            {
                return ex.Message.ToString();
            } 
        }
        public List<int>  WriteData(string filepath)
        {
            try
            {
                List<int> info = new List<int>();
                string[] lines = File.ReadAllLines(filepath);//读取文件中的所有行并将所有行放进string数组中
                for (int i = 0; i < lines.Length; i ++)
                {
                    int value = int.Parse(lines[i]);//遍历赋值,返回list的值
                    info.Add(value);
                }
                return info;
            }
           catch(Exception ex)
            {
                return null;
            }
        }
    }
}

关于“C# List如何生成Txt文档且读取Txt文档封装List”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI