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);
        }
    }
}
相关推荐
期待のcode3 分钟前
Java Object 类
java·开发语言
Wang's Blog10 分钟前
Lua: 协程编程详解之从基础到多任务处理与应用实战
开发语言·lua
笙枫14 分钟前
LangGraph Agent 架构基础:从概念到第一个可运行的Agent
开发语言·架构·php
csbysj202032 分钟前
DOM 验证
开发语言
superman超哥42 分钟前
Rust 表达式与语句的区别:函数式思维与控制流设计
开发语言·后端·rust·rust表达式·rust语句·函数式思维·控制流设计
趁月色小酌***1 小时前
JAVA 知识点总结5
java·开发语言·python
05大叔1 小时前
SpringMVCDay01
java·开发语言
代码游侠1 小时前
复习——网络测试工具
linux·开发语言·网络·笔记·学习·测试工具
Felven1 小时前
C. Contrast Value
c语言·开发语言·算法
我的xiaodoujiao1 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 37--测试报告 Allure 前置步骤-配置安装 JDK 详细图文教程
java·开发语言·学习·测试工具