c# - 如何将数组的内容写入文本文件? C#

标签 c# arrays file text

我正在尝试将数组的内容写入文本文件。我已经创建了文件,将文本框分配给数组(不确定是否正确)。现在我想将数组的内容写入一个文本文件。 streamwriter 部分是我被困在底部的地方。不确定语法。

if ((!File.Exists("scores.txt"))) //Checking if scores.txt exists or not
{
    FileStream fs = File.Create("scores.txt"); //Creates Scores.txt
    fs.Close(); //Closes file stream
}
List<double> scoreArray = new List<double>();
TextBox[] textBoxes = { week1Box, week2Box, week3Box, week4Box, week5Box, week6Box, week7Box, week8Box, week9Box, week10Box, week11Box, week12Box, week13Box };

for (int i = 0; i < textBoxes.Length; i++)
{
    scoreArray.Add(Convert.ToDouble(textBoxes[i].Text));
}
StreamWriter sw = new StreamWriter("scores.txt", true);

最佳答案

你可以这样做:

System.IO.File.WriteAllLines("scores.txt",
    textBoxes.Select(tb => (double.Parse(tb.Text)).ToString()));

关于c# - 如何将数组的内容写入文本文件? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13023147/

相关文章:

c# - asp.net 中的 CheckBox 控件 (C#)

c# - 从大量声音文件中播放

php - 带逗号的单独 PHP 数组的数据

php - 如何轻松地从数组中删除最后一个逗号?

c++ - 如何从 C++ 文件中读取由逗号分隔的全名?

linux - 我想在 fortran 中编辑我的 fortran 生成的文件

C++ 从包含字符的文件中读取数字

c# - 从 IOwinContext 获取路由模板

c# - 通过 Interop 调用返回结构实例的方法

javascript - gzip javascript 文件传输到浏览器更快?