C#读写问题?
吴青峰 2008-06-24 07:52:04 //查找事件
private void btnSearch_Click(object sender, EventArgs e)
{
//获取名字
string name = txtName.Text.Trim();
//读文件
StreamReader sr = null;
string custmerName = "";
string address = "";
string phone = "";
string lineString = "";
//标记
bool flag = false;
try
{
sr = new StreamReader(path);
while ((lineString = sr.ReadLine()) != null)
{//循环读文本
if (lineString != "")
{
string[] msg = lineString.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
if (msg[0].Equals("姓名"))
{
custmerName = msg[1];
if (custmerName.Equals(name))
{
flag = true;
}
else
{
flag = false;
}
}
if (flag)
{
if (msg[0].Equals("地址"))
{
address = msg[1];
}
if (msg[0].Equals("电话"))
{
phone = msg[1];
}
break;
}
}
}
if (flag)
{
MessageBox.Show("客户姓名:" + custmerName + " 地址:" + address + " 电话:" + phone);
}
else
{
MessageBox.Show("没有你要找的客户!!!");
}
}
catch (Exception ioe)
{
Debug.WriteLine(ioe.Message);
}
finally
{
//释放资源
if (sr != null)
{
sr.Close();
}
}
}
这段代码没有什么异常,就是这句代码很奇怪:
MessageBox.Show("客户姓名:" + custmerName + " 地址:" + address + " 电话:" + phone);
名字都出来了,address ,phone却不显示出来,为什么?请师傅帮忙