【发布时间】:2012-12-17 16:12:13
【问题描述】:
我正在尝试使用 StreamReader 在我的 .CSV 文件(它必须是 .csv 文件)上生成项目 ID。项目 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");
}
}
【问题讨论】:
标签: c# .net winforms linq stream