简体   繁体   English

这行代码在做什么? (编辑 .txt 文件中的一行 - ASP.NET MVC / C#)

[英]What is this line of code doing? (Editing a line within a .txt file - ASP.NET MVC / C#)

Below i have an action result that is suppose to perform a delete function on a specific line within a text file when a condition is met in order to execute a particular if statement.下面我有一个操作结果,假设满足条件时在文本文件中的特定行上执行删除 function 以执行特定的 if 语句。 Right now i believe i understand the majority of the lines and their specific functions, however i am unsure about a specific line and variable being created.现在我相信我了解大多数行及其特定功能,但是我不确定正在创建的特定行和变量。

Here is the ActionResult receiving an integer parameter containing a ID value:这是接收包含 ID 值的 integer 参数的 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.         }

I am struggling to understand what the purpose of the variable 'n' is, and why it is assigned "".我正在努力理解变量“n”的用途是什么,以及为什么将其分配为“”。 (Line 7), and then also i am not sure how the delete function works within the if statement on line 16. I have no idea why n is being incremented with strOldText. (第 7 行),然后我也不确定 delete function 如何在第 16 行的 if 语句中工作。我不知道为什么 n 会随着 strOldText 递增。

If anyone is able to explain this to me, i would greatly appreciate it.如果有人能够向我解释这一点,我将不胜感激。 Thank you so much!太感谢了!

You misunderstand line 14. The exclamation mark means 'not' .你误解了第 14 行。感叹号的意思是'not' So the while loop iterates over every line of the input file, adding it to the output except if the second field of the line maches the search string.因此,while 循环遍历输入文件的每一行,将其添加到 output 中,除非该行的第二个字段与搜索字符串匹配。 So it is effectively removing lines by not adding them to the output.因此,它通过不将它们添加到 output有效地删除线。

Line 7 is just initializing the variable that holds the output with an empty string.第 7 行只是用空字符串初始化包含 output 的变量。 This is to prevent NullReferenceException in the case that the input is empty.这是为了防止在输入为空的情况下出现 NullReferenceException。 It also prevents a compiler warning if you use an uninitialized variable.如果您使用未初始化的变量,它还可以防止编译器警告。

Line 7 7号线

This is assigned to using an empty string as if it was just var n;这被分配给使用一个空字符串,就好像它只是var n; it'll cause an error that basically says it cannot determine the type of n .它会导致一个错误,基本上说它无法确定n的类型。 So you assign an empty string to it.因此,您为其分配了一个空字符串。 This also prevents another error where the variable isn't assigned to (though, this error would only occur if you did string n; ).这也可以防止另一个没有分配变量的错误(尽管,这个错误只会在你执行string n;时发生)。

The variable itself is used to append text to.变量本身是用来给 append 文本的。 See line 16. If the text contains the search item, it'll append strOldText to n with a newline.请参见第 16 行。如果文本包含搜索项,它将 append strOldText到 n 并带有换行符。

Line 16 16 号线

Here, n isn't being incremented.在这里, n没有被递增。 It is being appended to as the value of n is string .它被附加到,因为n的值是string What += does is add the new text to the string without overriding the existing string . +=所做的是将新文本添加到字符串而不覆盖现有字符串 So n is having whatever the current value of strOldText is added to it.所以nstrOldText的当前值添加到其中。

I'm also not sure what you mean about "delete function" as nothing in that snippet is deleting anything.我也不确定您所说的“删除功能”是什么意思,因为该片段中的任何内容都没有删除任何内容。

Hope I helped shed light on the code.希望我能帮助阐明代码。

EDIT:编辑:

Here's some documentation to help:这里有一些帮助文档:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-operator https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-operator

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM