如何将数组的内容写入文本文件? C# [英] How to write contents of an array to a text file? C#

查看:601
本文介绍了如何将数组的内容写入文本文件? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数组的内容写入文本文件.我已经创建了文件,并为数组分配了文本框(不确定是否正确).现在,我想将数组的内容写入文本文件.流作家部分是我停留在底部的地方.不确定语法.

I'm trying to write the contents of an array to a text file. I've created the file, assigned text boxes to the array (not sure if correctly). Now I want to write the contents of the array to a text file. The streamwriter part is where I'm stuck at the bottom. Not sure of the syntax.

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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆