我有這段代碼,我想改進它。我想刪除轉到無法訪問的代碼
string NameOfFile = tbOpenFile.Text;
FileStream ReadStream = new FileStream(NameOfFile, FileMode.Open, FileAccess.ReadWrite);
StreamReader r_Read = new StreamReader(ReadStream);
i = 1;
lb1.Items.Clear();
for (; ; i++)
{
tbOpenFile.Text = r_Read.ReadLine();
lines[i] = tbOpenFile.Text;
lb1.Items.Add(lines[i]);
if (r_Read.EndOfStream.Equals(true))
goto next;
}
next:
{
textBox2.Text = r_Read.EndOfStream.ToString();
ReadStream.Close();
}
我試着使用如果
string NameOfFile = tbOpenFile.Text;
FileStream ReadStream = new FileStream(NameOfFile, FileMode.Open, FileAccess.ReadWrite);
StreamReader r_Read = new StreamReader(ReadStream);
i = 1;
lb1.Items.Clear();
for (; ; i++)
{
tbOpenFile.Text = r_Read.ReadLine();
lines[i] = tbOpenFile.Text;
lb1.Items.Add(lines[i]);
if (r_Read.EndOfStream.Equals(true))
{
textBox2.Text = r_Read.EndOfStream.ToString();
ReadStream.Close();
}
}
和喜歡嘗試像這樣使用而
string NameOfFile = tbOpenFile.Text;
FileStream ReadStream = new FileStream(NameOfFile, FileMode.Open, FileAccess.ReadWrite);
StreamReader r_Read = new StreamReader(ReadStream);
i = 1;
lb1.Items.Clear();
while(r_Read.EndOfStream.Equals(true))
{
for (; ; i++)
{
tbOpenFile.Text = r_Read.ReadLine();
lines[i] = tbOpenFile.Text;
lb1.Items.Add(lines[i]);
}
continue;
textBox2.Text = r_Read.EndOfStream.ToString();
ReadStream.Close();
}
但我總是得到一個錯誤這樣做
無法到達的代碼
代碼'繼續;'不執行 - 這是你無法訪問的代碼。 – user1666620
if(r_Read.EndOfStream.Equals(true))break; – Gusman
Goto's是**非常糟糕的做法。我想知道爲什麼這不是第一個評論。 – bytecode77