我创建了两个必须都使用csv文件的表格。我可以注册其他用户,然后登录一个配置文件(如果存在)。我使用StreamWriter添加信息:
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine(username + "," + password);
}
然后我阅读以下信息:
using (StreamReader reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] userData = line.Split(',');
usernames.Add(userData[0]);
passwords.Add(userData[1]);
}
}
和
bool isFound = false;
for (int i = 0; i < usernames.Count; i++)
{
if (usernames[i] == username && passwords[i] == password)
{
isFound = true;
}
}
我想向该行添加其他信息,例如图片位置,金额,车库空间,但是我从互联网上尝试过的所有内容都将数据写在新的行上,而不是现有的行上。我从讲师那里得到了一些提示-首先读取文件,然后再写入文件,但仍然无法正常工作。每当我添加数据时,我就使用
using(StreamWriter writer = new StreamWriter(filePath,true))
{
writer.Write("the username from the register from" + "," + "the password from the register form" + openFileDialog.FileName("the location of the file/What my lector told me to use"));
}
我只需要知道如何添加这些额外的数据,这样我就可以继续我的项目的其余部分。抱歉,如果您已经回答了这个问题,谢谢您的时间。
您不能使用replace
添加此信息吗?如果您具有用户名和密码,只需将其替换为相同的值以及其他信息