Ниже у меня есть результат действия, который предполагает выполнение функции удаления для указанной строки c в текстовом файле при выполнении условия для выполнения определенного оператора if. Сейчас я считаю, что понимаю большинство строк и их специфические c функции, однако я не уверен в том, что конкретная c строка и создаваемая переменная.
Вот ActionResult, получающий целочисленный параметр, содержащий значение идентификатора:
1. public ActionResult DeleteItemLine(int id) //recieve id of the delete button clicked
2. {
3. string strFilePath = "~/App_Data/item.txt"; //fetch file path (access file)
4. string strSearchText = id.ToString(); //assign the passed id to 'strSearchText' and convert to string
5. string strOldText; //create string variable 'strOldText' This will be the string responsible for holding each line within the text file at a particular time
6.
7. string n = ""; //NOT SURE WHAT THE PURPOSE OF THIS IS
8. StreamReader sr = System.IO.File.OpenText(Server.MapPath(strFilePath)); //open the text file specified by 'strFilePath'
9. while ((strOldText = sr.ReadLine()) != null) //run through entire text file, line-by-line, until the last line is reached (a null is encountered)
10. {
11.
12. string[] x = strOldText.Split(','); //create an array 'x' of type string, make this array split each word within a line, in the text file, when a comma is encountered
13.
14. if (!x[1].Contains(strSearchText)) //if the Primary Key within the item matches the id of the edit button pressed (strSearchText), then execute the following code
15. {
16. n += strOldText + Environment.NewLine; //NOT SURE HOW THIS WORKS
17. }
18. }
19. sr.Close(); //closes the StreamReader
20. System.IO.File.WriteAllText(Server.MapPath(strFilePath), n); //writes the updated text file to the specified directory containing the text file originally opened and read
21.
22. return RedirectToAction("Index"); //not important right now
23. }
Я изо всех сил пытаюсь понять, какова цель переменной 'n' и почему она назначена "". (Строка 7), а затем я также не уверен, как работает функция удаления в операторе if в строке 16. Я понятия не имею, почему n увеличивается с помощью strOldText.
Если кто-то может объяснить это для меня, я был бы очень признателен. Большое вам спасибо!