.CSVファイル(.csvファイルである必要があります)でStreamReaderを使用してアイテムIDを生成しようとしています。アイテムIDは1000から始まり、上がる必要があります(1001、1002など)。
現在、ユーザーが「Generate 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");
}
}