在 C# 中截断 CSV 文件

发布于 2024-10-05 06:59:07 字数 1251 浏览 0 评论 0原文

我有一个包含 48 行整数的 CSV 文件。我正在使用 Visual C# 的 openfiledialog 功能来允许用户选择此文件。然后我想让程序将该文件截断为 24 行。是否有一个截断函数可以用来轻松完成此操作?如果不是,我该如何去做呢?以下是我到目前为止所拥有的......

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sts_converter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void select_Click(object sender, EventArgs e)
        {
            int size = -1;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                try
                {
                    string text = File.ReadAllText(file);
                    size = text.Length;
                }
                catch (IOException)
                {
                }
            }
            Console.WriteLine(size); // <-- Shows file size in debugging mode.
            Console.WriteLine(result); // <-- For debugging use only.
        }

    }
}

I have a CSV file with 48 rows of integers. I am using the openfiledialog feature of visual c# to allow a user to select this file. I then want to have the program truncate that file down to 24 rows. Is there a truncate function I can use to do this easily? If not how can I go about doing so? Below is what I have so far...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sts_converter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void select_Click(object sender, EventArgs e)
        {
            int size = -1;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                try
                {
                    string text = File.ReadAllText(file);
                    size = text.Length;
                }
                catch (IOException)
                {
                }
            }
            Console.WriteLine(size); // <-- Shows file size in debugging mode.
            Console.WriteLine(result); // <-- For debugging use only.
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉墨 2024-10-12 06:59:07

它可能很简单:

string file = openFileDialog1.FileName;
File.WriteAllLines(
    file, 
    File.ReadLines(file).Take(28).ToArray()
);

It could be as easy as:

string file = openFileDialog1.FileName;
File.WriteAllLines(
    file, 
    File.ReadLines(file).Take(28).ToArray()
);
高跟鞋的旋律 2024-10-12 06:59:07
  • 调用 ReadAllLines 获取 48 个字符串的数组
  • 创建一个包含 24 个字符串的新数组
  • 使用 Array.Copy 复制所需的字符串
  • 调用 WriteAllLines将新数组写入文件

(您也可以使用 LINQ 的 Take 方法)

  • Call ReadAllLines to get an array of 48 strings
  • Create a new array of 24 strings
  • Use Array.Copy to copy the strings that you want
  • Call WriteAllLines to write the new array to the file

(You can also use LINQ's Take method)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文