這是你們在這裏得到的最常見的問題之一,所以原諒我,但我無法克服不管我多麼努力地搜索。C#創建一個類列表<>/Dictionary <>,寫入和讀取.txt文件作爲數據庫,不會覆蓋數據
我建立在Windows項目中的應用鑰匙扣窗體應用程序,併爲儘可能方便地理解這一概念的目的,我用List <去>而不是字典// ILIST
這是類我一直在使用:
public class Account
{
public static List<Account> myAccountList = new List<Account>();
public string Domain; //this is supposed to be google/skype/facebook
public string Email;
public string Username;
public string Password;
public Account (string domain, string email, string username, string password)
{
Domain = domain;
Email = email;
Username = username;
Password = password;
myAccountList.Add(new Account(domain, email, username, password)); //constructor calls the new list instance
}
private static void SaveToFile()
{
System.IO.File.WriteAllLines(@accountdb.txt, myAccountList);
}
private static void ReadFromFile() // this is meant to be used as authentication in my main form, it isn't necessary right now..
{
System.IO.File.ReadAllLines(@accountdb.txt);
}
}
我有幾個問題與此:
- 我不能創建一個功能保存方法寫科幻le,我已經嘗試了幾種方法
System.IO
- 當我將列表更改爲一維數組時,它會不斷覆蓋它,我想模擬MySQL以便以後熟悉。
調用構造函數的按鈕單擊事件:
private void buttonSave_Click(object sender, EventArgs e)
{
string domain, email, username, password;
domain = comboboxDomain.Text;
email = textboxEmail.Text;
username = textboxUsername.Text;
password = textboxPassword.Text;
//Checking for correct EMAIL
if (!textboxEmail.Text.Contains("@") && (!textboxEmail.Text.Contains(".")))
{
MessageBox.Show("Invalid Email Format");
}
else
{
Account account = new Account(domain, email, username, password);
}
}
好的,那你的問題是什麼? – maccettura
@maccettura我想知道我寫入列表到文件的方式有什麼問題,以及如何防止保存方法覆蓋.txt文件中的以前憑據 –