Привет, я новичок в C # и могу сделать с некоторой помощью.У меня есть программа, которая в настоящее время сохраняет события в своем собственном текстовом файле, который содержит подробную информацию о событии (детали вводятся пользователем).Одной из таких деталей является количество билетов.У меня возникают проблемы с тем, чтобы при получении билета сумма уменьшалась на единицу.
Я хотел бы знать, есть ли для меня возможность вычесть 1 из числа в текстовом файле.Вот как устроены мои текстовые файлы:
Event Name: Test
Event Time: 12:30
Event Location: Test
Amount Of Tickets: 120
Price Of Tickets: £5
Это метод, который я попробовал, и все, что он сделал, это добавил -1 -1 к значению вместо того, чтобы убирать значение:
Console.WriteLine("What Event Would You Like To Buy A Ticket For?");
string EventUpdate = Console.ReadLine();
string folderPath = (@"A:\Work\Visual Studio\TextFiles");
string fileName = EventUpdate + ".txt";
string filePath = folderPath + "\\" + fileName; //creats file path using FolderPath Plus users Input
string Contents = File.ReadAllText(filePath);
Console.WriteLine(Contents); //displays the txt file that was called for
Console.WriteLine("\n");
string FindText = "Amount Of Tickets:";
int I = -1;
string NewText = FindText + I;
string NewTempFile = folderPath + EventUpdate + ".txt";
string file = filePath;
File.WriteAllText(file, File.ReadAllText(file).Replace(FindText, NewText));
using (var sourceFile = File.OpenText(file))
{
// Create a temporary file path where we can write modify lines
string tempFile = Path.Combine(Path.GetDirectoryName(file), NewTempFile);
// Open a stream for the temporary file
using (var tempFileStream = new StreamWriter(tempFile))
{
string line;
// read lines while the file has them
while ((line = sourceFile.ReadLine()) != null)
{
// Do the Line replacement
line = line.Replace(FindText, NewText);
// Write the modified line to the new file
tempFileStream.WriteLine(line);
}
}
}
// Replace the original file with the temporary one
File.Replace(NewTempFile, file, null);
это то, что случилось с моим текстовым файлом, когда я использовал приведенный выше код:
Event Name: Test
Event Time: 12:30
Event Location: Test
Amount Of Tickets:-1-1 120
Price Of Tickets: £5