基本信息
源码名称:C#实现文件读写
源码大小:0.42M
文件格式:.zip
开发语言:C#
更新时间:2020-07-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍
简单的实现ini、CSV、txt文件读写方法

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 WindowsFormsApp7
{
    public partial class frmCsv : Form
    {
        private DataTable dt = new DataTable();

        public frmCsv()
        {
            InitializeComponent();

            dataGridView1.DataSource = dt;
        }

        private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                ChangeRowCount();
            }
        }

        private void ChangeRowCount()
        {
            int rowCount = int.Parse(tb_RowCnt.Text);

            while (rowCount > dt.Rows.Count)
            {
                dt.Rows.Add();  //增加行
                dt.Rows[dt.Rows.Count - 1][0] = dt.Rows.Count;
            }
            while (rowCount < dt.Rows.Count)
            {
                dt.Rows.RemoveAt(dt.Rows.Count - 1);  //删除行
            }
        }

        private void Bt_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "打开CSV文件";
            fd.Filter = "CSV文本文件 (*.csv)|*.csv";
            //fd.FilterIndex = 1;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                lb_Path.Text = "CSV路径:" fd.FileName;
                FileOperate.CSV csv = new FileOperate.CSV(fd.FileName);
                dt = csv.ReadData();
                dataGridView1.DataSource = dt;
                tb_RowCnt.Text = dt.Rows.Count.ToString();
                tb_RowCnt.ReadOnly = false;
                MessageBox.Show(fd.FileName "导入成功!");
            }
        }

        private void Bt_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();
            fd.Title = "保存CSV文件";
            fd.Filter = "All Files (*.*)|*.*";
            //fd.FilterIndex = 1;
            string saveName = "*.csv";
            if (lb_Path.Text.Remove(0, 6) != "") saveName = lb_Path.Text.Substring(lb_Path.Text.LastIndexOf('\\') 1);
            fd.FileName = saveName;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                FileOperate.CSV csv = new FileOperate.CSV(fd.FileName);
                csv.WriteData(dt);
                lb_Path.Text = "CSV路径:" fd.FileName;
                MessageBox.Show(fd.FileName "保存成功!");
            }
        }
    }
}