我正在尝试在我的 .CSV 文件上使用 StreamReader 生成项目 ID(它必须是 .csv 文件)。项目 ID 应从 1000 开始并上升(1001、1002 等)
现在,如果用户按下“生成 ID”,它将在整个文件中搜索值“1000”,如果不存在,它将在文本框中写入“1000”。
这是我需要帮助的内容:如果文件包含“1000”,我希望它读取最后一行,将其增加 1,然后将值写入文本框中。所以,如果我的最后一个值是 .csv 中的 1005文件,我希望它在文本框中写入 1006。
private void GenerateID_Click(object sender, EventArgs e)
{
try
{
string searchString = "1000";
using (StreamReader sr = new StreamReader("file.csv"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(searchString))
{
/* If file contains 1000, read the LAST line
* (Whatever number that may be: 1001, 1002, 1003, etc.)
* and increase that number by 1, then write to textbox. */
}
else
{
invItemIDField.Text = Convert.ToString("1000");
}
}
}
}
catch (Exception)
{
MessageBox.Show("The file could not be read");
}
}