I need to read data from a file, so I have this code.
public static void readData()
{
TextReader readFile;
string chain;
bool found = false;
string[] fields = new string[2];
char[] breakUp = { ':' };
try
{
readFile = new StreamReader("C:\\Users\\unam\\Documents\\Hugo\\Datos.txt");
chain = readFile.ReadLine();
while (chain != null)
{
fields = chain.Split(breakUp);
if (fields[0].Trim().Equals("Name"))
{
name = fields[1].Trim();
}
else
{
if (fields[0].Trim().Equals("Age"))
{
Age = fields[1].Trim();
}
}
readFile.Close();
}
}
catch(FileNotFoundException fe)
{
Console.WriteLine("¡File not found!" + fe.Message);
}
catch(Exception e)
{
Console.WriteLine("¡File not found!" + e.Message);
}
}
And in the document I have
- Name: Hugo
- Age: 23
When I execute the code it stays in a loop in the first if so it just reads the name again and again, my question is, how can I get out of that loop to read the other data?
chain
. Don't let it stay the same value or nothing changes.StreamReader
/TextReader
in ausing
block.