如何将文本文件中的值分配给另一个值?

问题描述 投票:0回答:0

我目前有非常基本的代码,允许我将我想要的任何内容写入文本文件,然后在我退出应用程序后保存。我可以通过简单地再次运行程序并在重复该过程之前通过 ReadLine() 添加更多行来向该文件添加额外的行。

但我不相信使用这段代码,程序可以识别我的文本文件中的不同“项目”。

static void Main(string[] args)
        {
            if (File.Exists("test.txt"))
            {
                string items = File.ReadAllText("test.txt");
                Console.WriteLine("Current inventory stock:");
                Console.WriteLine(content);
            }
            Console.WriteLine("Please enter a new tool into the inventory: ");
            string newItems = Console.ReadLine();
            while (newItems != "exit")
            {
                File.AppendAllText("test.txt", newItems + Environment.NewLine);
                newItems = Console.ReadLine();
            }
        }

我的文本文件看起来像这样(所有占位符名称):

ItemOne, ItemInfo, 2
ItemTwo, ItemInfo, 6
ItemThree, ItemInfo, 4
ItemFour, ItemInfo, 1
ItemFive, ItemInfo, 7

我正在尝试获取 int 值以匹配其相应的项目。所以“2”应该是“ItemOne”的一部分,而不是其他的。有没有这方面的教程或资源/信息?

我创建了一个类似的程序,但这要容易得多,因为文本文件是只读的,所以我不必处理这个。

c# console-application
© www.soinside.com 2019 - 2024. All rights reserved.