从C#保存到CSV文件。

人气:921 发布:2022-09-22 标签: c# console

问题描述

我有想要为其创建程序的数据(马名列表)。我想要创建的程序会将马名保存到csv文件中但是我希望能够限制每次传输到文件的马名称的数量,有100个名称,我希望有时能够选十,然后另一次选十五?我试图通过控制台应用程序执行此操作,并希望在编码时输入匹配的名称,而不是通过控制台,我只想将名称随机生成到csv文件中。它的表演。 有谁可以帮我这么做?

Hi, I have data(list of horse names) that I want to create a program for. The program I want to create will save the horse names to a csv file but then I want to be able to limit the number of horse names that are transferred to the file each time, there's 100 names, I want to be able to at times pick ten, then another time pick fifteen? I'm trying to do this via a console application and want to input the horses names at the time of coding, instead of via the console, I just want to randomly generate the names into a csv file. Its for a show. Can anyone help me do this?

推荐答案

如果你只想要马的名字,那么可能CSV有点矫枉过正 - 一个简单的列表,每行一个名称很容易读写,不需要特殊处理: 假设你的名字在一个数组中(只有你感兴趣的十个) ) 写道: If you just want the horse names, then possibly CSV is a little overkill - a simple list with one name per line is easy to read and write and needs no special processing: Assuming you have your names in an array (just the ten you are interested in) Write:
File.WriteAllLines(path, horses);

阅读:

Read:

string[] horses = File.ReadAllLines(path);

如果您必须使用CSV,请参阅此处:阅读和写作C#中的CSV文件 [ ^ ]用于快速简单的版本。

If you have to use CSV, then see here: Reading and Writing CSV Files in C#[^] for a quick and simple version.

733