C#文件操作

一、txt文件

1. 写入txt文件

cs 复制代码
File.AppendAllText(@"C:\test.txt", text);  // 向后追加
File.WriteAllText(@"C:\test.txt", text);   // 覆盖之前的数据

2. 读取txt文件

cs 复制代码
string str = File.ReadAllText(@"C:\test.txt"); 

3. 获取当前程序的运行目录

cs 复制代码
string path = Directory.GetCurrentDirectory();

二、 创建文件夹

cs 复制代码
Directory.Exists(path);  // 判断文件夹是否存在
Directory.CreateDirectory(path);  // 创建文件夹

三、csv操作

1. 写入csv文件

cs 复制代码
tring path = @"C:\data.csv";
using(StreamWriter sw = new StreamWriter(path, true, Encoding.Default)){
  StringBuilder sb = new StringBuilder();  // 创建一个字符串实例
  // 写入数据
  sb.Append(textBox1.Text).Append(textBox2.Text).Append(textBox3.Text)
  sw.WriteLine(sb);
  sw.Close();
  sw.Dispose();
}

四、csv封装

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

namespace IcCogFrameGrabber连相机
{
    public class FileOperate
    {

        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;
        }
    }
}

五、自创csv写入方法

cs 复制代码
string data = "1,2,3,4,5\n";
File.AppendAllText(path + "\\data.csv", data);
MessageBox.Show("111");
相关推荐
爱编程的鱼7 分钟前
C# 结构(Struct)
开发语言·人工智能·算法·c#
只可远观19 分钟前
Flutter Dart 循环语句 for while do..while break、continue
开发语言·javascript·ecmascript
是阿根43 分钟前
unity使用iTextSharp生成PDF文件
unity·c#·游戏引擎
吴_知遇1 小时前
【华为OD机试真题】428、连续字母长度 | 机试真题+思路参考+代码解析(E卷)(C++)
开发语言·c++·华为od
basketball6162 小时前
Python torchvision.transforms 下常用图像处理方法
开发语言·图像处理·python
ABAP 成2 小时前
.NET Framework 4.0可用EXCEL导入至DataTable
c#
宁酱醇2 小时前
各种各样的bug合集
开发语言·笔记·python·gitlab·bug
啊吧怪不啊吧2 小时前
Linux常见指令介绍下(入门级)
linux·开发语言·centos
谷晓光2 小时前
Python 中 `r` 前缀:字符串处理的“防转义利器”
开发语言·python
Tiger Z2 小时前
R 语言科研绘图第 41 期 --- 桑基图-基础
开发语言·r语言·贴图