C# CSV文件读写

API

cs 复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSV
{
    public class CSVApi
    {

        public void SaveData(string data, string result)
        {

            string path = Directory.GetCurrentDirectory() + "\\Data";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            //string fileName = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".csv";
            string fileName = $"{path}\\{DateTime.Now.ToString("yyyy-MM-dd")}.csv";
            if (!File.Exists(fileName))
            {
                //创建文件流,传入文件路径,创建并写入
                FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                //Encoding.Default 编码格式 允许写入中文内容
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                //创建StringBuilder类型追加写入信息
                StringBuilder sb = new StringBuilder();
                sb.Append("时间").Append(",").Append("Data").Append(",").Append("Result");
                sw.WriteLine(sb);
                sw.Close();
                sw.Dispose();
                fs.Close();
                fs.Dispose();

            }
            using (StreamWriter sw2 = new StreamWriter(fileName, true, Encoding.Default))
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(DateTime.Now.ToString("HH-mm-ss")).Append(",").Append(data).Append(",").Append(result);
                sw2.WriteLine(sb);
            }

        }

        public string ReadData()
        {
            string path = Directory.GetCurrentDirectory() + "\\Data";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            //string fileName = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".csv";
            string fileName = $"{path}\\{DateTime.Now.ToString("yyyy-MM-dd")}.csv";

            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string str = sr.ReadToEnd();
            return str;
        }
    }
}

调用

cs 复制代码
using CSV;
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 CSV文件读写
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        CSVApi cSVApi = new CSVApi();
        private void button1_Click(object sender, EventArgs e)
        {
            cSVApi.SaveData("张三","李四");
        }

        private void button2_Click(object sender, EventArgs e)
        {
           string value = cSVApi.ReadData();
            MessageBox.Show(value);
        }
    }
}
相关推荐
ftpeak8 分钟前
Python win32底层开发从入门到实战
开发语言·python·win32api
阿正的梦工坊10 分钟前
JavaScript 函数组合(Compose & Pipe)详解
开发语言·javascript·网络
lly20240611 分钟前
Python uWSGI 安装配置
开发语言
两年半的个人练习生^_^27 分钟前
每日一学:设计模式之原型模式
java·开发语言·设计模式·原型模式
elseif12328 分钟前
初学者必背【考点清单(大全)】【上篇】
开发语言·c++·笔记·学习·循环结构·分支结构·考纲
并不喜欢吃鱼28 分钟前
从零开始C++----二.(下篇)模版进阶与编译全过程的复习
开发语言·c++
234710212731 分钟前
4.17 学习笔记
开发语言·软件测试·笔记·python·学习
不知名的老吴1 小时前
View的三大特性之一:迟绑定
开发语言·c++·算法
深邃-1 小时前
【Web安全】-基础环境安装:虚拟机安装,JDK环境安装(1)
java·开发语言·计算机网络·安全·web安全·网络安全·安全架构
前端老石人1 小时前
前端网站换肤功能的 3 种实现方案
开发语言·前端·css·html