There are a few things wrong with this: lets start with a biggie!
ThanhPho = sr.ReadLine();
do
{
...
} while (ThanhPho != null);
ThanhPho = sr.ReadLine();
sr.Close();
This reads a line from a file; processes it, and if it exists, goes around to process it again. And again. And again. If it doesn't exist, it read another line (from the file that already must be empty) and then closes the file and exits the routine.
What you actually meant to say was:
while ((ThanhPho = sr.ReadLine()) != null);
{
...
}
sr.Close();
Which will process more lines, and may fix at least one of your problems.
Why are you opening a stream, then passing it to another stream?
Open it, read it, close it, dispose it. The easiest way to do that for your application is just to use File.ReadAllLines:
string[] lines = File.ReadAllLines(D:\\City.txt");
foreach (string ThanhPho in lines)
{
...
}
If you must use streams, then enclose them in
using
blocks, to ensure they are closed and disposed properly.
This isn't an error, but it will make it easier to read: replace your
ThanhPho.Substring(0, 1) == "T"
with
ThanhPho.StartsWith("T")
or even
ThanhPho.StartsWith("Tp:")
This isn;t quite an error, but it is a very good idea: Always use curly brackets around any if...else... statement - that way if you add a statement later, they are already there and you don't end up with the confusion your else could have. I.e.
if(ThanhPho.StartsWith("T"))
{
...
}
else
{
Console.WriteLine("Khong co");
}
Without knowing more about the definition of "City" it is difficult to help further!