Bon*_*rds 3 c# arrays text file
我正在尝试将数组的内容写入文本文件.我已经创建了文件,为文件分配了文本框(不确定是否正确).现在我想将数组的内容写入文本文件.写行者部分是我被困在底部的地方.不确定语法.
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);
Run Code Online (Sandbox Code Playgroud)
Eni*_*ity 12
你可以这样做:
System.IO.File.WriteAllLines("scores.txt",
textBoxes.Select(tb => (double.Parse(tb.Text)).ToString()));
Run Code Online (Sandbox Code Playgroud)
using (FileStream fs = File.Open("scores.txt"))
{
StreamWriter sw = new StreamWriter(fs);
scoreArray.ForEach(r=>sw.WriteLine(r));
}
Run Code Online (Sandbox Code Playgroud)