First, DO NOT USE GOTO! There's absolutely NO REASON the code you posted needs to have GOTO statements. Write proper code. I haven't used a GOTO since 1978. It's a hold-over from interpreted basic, and should be avoided at all costs.
Second, here's my version of your method (assuming I have correctly determined what you're trying to do).
private static void button21_Click(object sender, EventArgs e)
{
string line = string.Empty;
StringBuilder text = new StringBuilder();
List<string> fileContent = new List<string>();
int lineCounter = 0;
try
{
using (StreamReader sr = new StreamReader(@"c:\temp\Input.txt"))
{
fileContent.AddRange(sr.ReadToEnd().Replace("\r\n", "\n").Split('\n'));
}
if (fileContent.Count > 0)
{
do
{
line = fileContent[lineCounter];
text.AppendLine(line);
lineCounter++;
} while ((lineCounter < fileContent.Count) &&
(lineCounter <= 5 ||
line.Contains(" End") ||
line.Trim().StartsWith("End")));
using (TextWriter sw = File.CreateText(@"c:\temp\Output.txt"))
{
sw.Write(text.ToString());
}
}
}
catch (Exception ex)
{
string error = string.Concat(ex.Message,
Environment.NewLine,
Environment.NewLine,
ex.StackTrace);
Console.WriteLine(error);
}
}