C# 文件读写 程序

news/2024/10/22 16:44:51/

//读写文件
public class ReadWriteFile
{
    public static List<string> ReadFile(string path)
    {
        try
        {
            using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                List<string> list = new List<string>();
                using (StreamReader streamReader = new StreamReader(fileStream))
                {
                    while (!streamReader.EndOfStream)
                    {
                        list.Add(streamReader.ReadLine());
                    }
                    return list;
                }
            }
        }
        catch (Exception)
        {
            throw new Exception("read error!");
        }
    }

    public static void WriteFile(string path, List<string> list)
    {
        try
        {
            using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter streamWriter = new StreamWriter(fileStream))
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        streamWriter.WriteLine(list[i]);
                    }
                }
            }
        }
        catch (Exception)
        {
            throw new Exception("write error!");
        }
    }
}
    
//列表和表格转换
public static class ListViewConvert
{
    public static void ListToView(List<string> list, DataGridView dataGridView)
    {
        dataGridView.Columns.Clear();
        string[] s = list[0].Split(',');
        for (int i = 0; i < s.Length; i++)
        {
            dataGridView.Columns.Add(s[i], s[i]);
        }

        for (int i = 0; i < list.Count - 1; i++)
        {
            dataGridView.Rows.Add();
            s = list[i + 1].Split(',');
            for (int j = 0; j < s.Length; j++)
            {
                dataGridView.Rows[i].Cells[j].Value = s[j];
            }
        }
    }

    public static List<string> ViewToList(DataGridView dataGridView)
    {
        List<string> list = new List<string>();

        string str = dataGridView.Columns[0].Name.ToString();
        for (int i = 1; i < dataGridView.Columns.Count; i++)
        {
            str = str + "," + dataGridView.Columns[i].Name.ToString();
        }
        list.Add(str);

        for (int i = 0; i < dataGridView.Rows.Count - 1; i++)
        {
            str = dataGridView.Rows[i].Cells[0].Value.ToString();
            for (int j = 1; j < dataGridView.Columns.Count; j++)
            {
                str = str + "," + dataGridView.Rows[i].Cells[j].Value.ToString();
            }
            list.Add(str);
        }
        return list;
    }
}

//界面显示
private void btnRead_Click(object sender, EventArgs e)
{
    try
    {
        List<string> list = ReadWriteFile.ReadFile(@"C:\Users\12072\Desktop\22.csv");
        ListViewConvert.ListToView(list, dataGridView1);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void btnWrite_Click(object sender, EventArgs e)
{
    try
    {
        List<string> list = ListViewConvert.ViewToList(dataGridView1);
        ReadWriteFile.WriteFile(@"C:\Users\12072\Desktop\33.csv", list);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


http://www.ppmy.cn/news/1000578.html

相关文章

【Java多线程学习5】什么是悲观锁,什么是乐观锁?如何实现乐观锁、乐观锁存在哪些问题

【Java多线程学习5】什么是悲观锁&#xff0c;什么是乐观锁&#xff1f;如何实现乐观锁、乐观锁存在哪些问题 一、什么是悲观锁 概述 悲观锁总是假设最坏的情况&#xff0c;认为共享资源每次被访问的时候就会出现问题(比如共享数据被修改)&#xff0c;所以每次在获取资源操作…

docker: Error response from daemon: No command specified.

执行 docker run -it -d -v /home/dell/workspace/workspace/test_192.168.1.202_pipeline:/home/workspace1 --name test_192.168.1.202_pipeline_10 qnx:7.1报错 问题定位&#xff1a;export导入的镜像需要带上command&#xff0c;以下命令查看command信息 docker ps --no…

RelativeSource有四种类型

Self FindAncestor TemplatedParent PreviousData a.Self Self用于绑定源和绑定目标相同的场景中。对象的一个属性与同一对象的另一个属性绑定。 例如&#xff0c;让我们取一个高度和宽度相同的椭圆。在XAML文件中添加下面给出的代码。宽度属性与高度属性相对绑定。 <G…

【搜索】DFS搜索顺序

算法提高课笔记 目录 马走日题意思路代码 单词接龙题意思路代码 分成互质组题意思路代码 马走日 原题链接 马在中国象棋以日字形规则移动。 请编写一段程序&#xff0c;给定 n∗m 大小的棋盘&#xff0c;以及马的初始位置 (x&#xff0c;y)&#xff0c;要求不能重复经过棋盘…

Python的字典使用

今天做力扣上1207. 独一无二的出现次数添加链接描述时用到了python字典&#xff0c;于是把字典的用法整理了一下。 新建字典 iters {}检查字典中是否含有某一个键 iters.has_key(key)字典根据键访问值 iters[key]遍历字典的键和值 for key,value in iters.items():整体代码 c…

Ubuntu20.04之VNC的安装与使用

本教程适用于Ubuntu20.04及以下版本&#xff0c;Ubuntu22.04版本或有出入 更多更新的文章详见我的个人博客&#xff1a;【前往】 文章目录 1.安装图形桌面1.1选择安装gnome桌面1.2选择安装xface桌面 2.安装VNC-Server3.配置VCN-Server4.连接VNC5.设置VNC-Server为系统服务&…

【数据结构】二叉树、二叉搜索树、平衡二叉树、红黑树、B树、B+树

概述 二叉树&#xff08;Binary Tree&#xff09;&#xff1a;每个节点最多有两个子节点&#xff08;左子节点和右子节点&#xff09;&#xff0c;没有限制节点的顺序。特点是简单直观&#xff0c;易于实现&#xff0c;但查找效率较低。 二叉搜索树&#xff08;Binary Search…

nmake编译Qt第三方库出现无法打开包含文件type_traits

最近需要为个人项目ShaderLab添加内嵌的代码编辑窗口功能&#xff0c;支持语法高亮和Intellisense&#xff0c;最初使用了QCodeEditor,发现这个第三方的库对词法分析的实现效果不太行. 代码换行后直接缩进到首行&#xff0c;无法定位到前一句的首行 考虑换QScintilla&#xff…