我目前正在测试一个将用户的联系方式存储在文件中的应用程序。此信息也作为主要方法存储在本地紧凑型数据库中 - 将其详细信息存储在此文件中是一种备份,以防这些详细信息丢失。
我用于测试的文件中有我的个人数据,所以我希望你明白我已经用占位符替换了这些行!该文件的结构如下(减去第一行):
File:
Business Name
Mr.
Joe
Bloggs
user@email.com
Address Line 1
Address Line 2
City
Postcode
Country
07777123456
下面,我有一些代码可以读取这个文件并将每一行存储为一个变量。文件的结构永远不会改变,因此代码非常简单:
public static bool RestoreBusinessTable(out string title, out string busName, out string mobileNumber, out string firstName, out string lastName)
{
string email = "", referral = "", contactNo, addressLine1 = "", addressLine2 = "", city = "", postcode = "", country = "", district = "";
busName = null;
mobileNumber = null;
firstName = null;
lastName = null;
title = null;
try
{
if (!File.Exists(fileName))
return false;
StreamReader sr = new StreamReader(fileName);
string work;
work = sr.ReadLine(); // Empty line
work = sr.ReadLine(); // Empty line
busName = sr.ReadLine();
title = sr.ReadLine();
firstName = sr.ReadLine();
lastName = sr.ReadLine();
email = sr.ReadLine();
referral = sr.ReadLine();
addressLine1 = sr.ReadLine();
addressLine2 = sr.ReadLine();
city = sr.ReadLine();
postcode = sr.ReadLine();
country = sr.ReadLine();
work = sr.ReadLine(); // Empty line
work = sr.ReadLine(); // Empty line
contactNo = sr.ReadLine();
district = sr.ReadLine();
mobileNumber = sr.ReadLine();
sr.Close();
// Add to database here
return true;
}
catch
{
return false;
}
}
运行此代码时,我注意到 、busName
、title
和firstName
alllastName
的值为07777123456
。数据如下所示:
07777123456
07777123456
07777123456
07777123456
user@email.com
Address Line 1
Address Line 2
City
Postcode
Country
07777123456
我没有任何异步进程或线程同时写入文件。谁能解释这里发生了什么,以及为什么前四行会显示为手机号码?