gpt4 book ai didi

c# - 我正在尝试将字符串数组保存到 txt 文件

转载 作者:行者123 更新时间:2023-12-04 08:23:35 25 4
gpt4 key购买 nike

我正在设计一个程序来保存食物食谱。问题是当我保存新配方的成分时,它会删除旧配方的成分。我希望他们两个都得救。这是我使用的代码:

Console.WriteLine("Enter the path where your files will be saved: ");

string fileName = Console.ReadLine();

Console.WriteLine("Please enter the main ingrediant for your recipe: ");

string Main = Console.ReadLine();

Console.WriteLine("Please enter how many ingrediants in your recipe: ");
int Ingrediants = Convert.ToInt32(Console.ReadLine());

string[] a = new string[Ingrediants];

Recpies recipe1 = new Recpies(Main, Ingrediants);
recipe1.Ingrediants = new List<string[]>();
recipe1.TheMainIngrediant = Main;

Console.WriteLine("Please enter your ingrediants: ");
for (int i = 0; i < Ingrediants; i++)
{
a[i] = Console.ReadLine();

}
using (var file = System.IO.File.OpenText(fileName))
{
string line;
while ((line = file.ReadLine()?.Trim()) != null)
{
// skip empty lines
if (line == string.Empty)
{
recipe1.Ingrediants.Add(a);
recipe1.Saving(fileName, Ingrediants, a);
}
else
{
continue;
}

}
}

最佳答案

实现您想要的一种可能的方法是使用 StreamWriter 之间的组合。和 File.AppendText ,这不会覆盖 text您已经保存了该文件,而不是将信息附加到文件末尾(此示例可能会有所帮助: Append Text )。希望这可以帮助! 👍

关于c# - 我正在尝试将字符串数组保存到 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65381863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com