2017-07-18 8 views
0

這是你們在這裏得到的最常見的問題之一,所以原諒我,但我無法克服不管我多麼努力地搜索。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); 
    } 
} 

我有幾個問題與此:

  1. 我不能創建一個功能保存方法寫科幻le,我已經嘗試了幾種方法System.IO
  2. 當我將列表更改爲一維數組時,它會不斷覆蓋它,我想模擬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); 
     } 
    } 
+0

好的,那你的問題是什麼? – maccettura

+0

@maccettura我想知道我寫入列表到文件的方式有什麼問題,以及如何防止保存方法覆蓋.txt文件中的以前憑據 –

回答

0

WriteAllLines期望一個string[]作爲第二個參數。因此,您需要創建一種將單個Account對象表示爲字符串的方法。最簡單的方法是覆蓋ToString方法。然後使用Linq Select方法選擇它們。

此外,如果Account類不包含帳戶列表會更好。這應該是一個單獨的課程。試試這樣的:

void Main() 
{ 
    var accountList = new AccountList(); 

    //Save a few accounts to the file 
    Account a1 = new Account("Google", "GoogleEmail", "GoogleUser", "GooglePass"); 
    accountList.Add(a1); 
    Account a2 = new Account("Netflix", "NetflixEmail", "NetflixUser", "NetflixPass"); 
    accountList.Add(a2); 

    AccountList.SaveToFile(@"g:\test\accounts.txt", accountList); 


    //Load the accounts from the file 
    AccountList aList2 = AccountList.ReadFromFile(@"g:\test\accounts.txt"); 
    aList2.Dump(); 
} 

public class AccountList 
{ 
    private List<Account> accounts; 
    public IEnumerable<Account> Accounts { get { return accounts;} } 

    public AccountList() 
    { 
     this.accounts = new List<Account>(); 
    } 

    public void Add(Account a) 
    { 
     accounts.Add(a); 
    } 

    public static void SaveToFile(string filename, AccountList accounts) 
    { 
     //Selects all the `Account` instances and creates a string[] 
     System.IO.File.WriteAllLines(filename, accounts.Accounts.Select(a => $"{a}").ToArray()); 
    } 

    public static AccountList ReadFromFile(string filename) // this is meant to be used as authentication in my main form, it isn't necessary right now.. 
    { 
     var result = new AccountList(); 

     //Read each line from the file and create an Account instance. 
     foreach (var line in System.IO.File.ReadAllLines(filename)) 
     { 
      if (!string.IsNullOrWhiteSpace(line)) 
      { 
       var parts = line.Split(','); 
       if (parts.Length == 4) 
       { 
        Account a = new Account(parts[0], parts[1], parts[2], parts[3]); 
        result.Add(a); 
       } 
      } 
     } 

     return result; 
    } 
} 

public class 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; 
    } 

    public override string ToString() 
    { 
     return $"{Domain},{Email},{Username},{Password}"; 
    } 
} 
+0

對於我目前的理解,這是非常先進的,但我毫不懷疑它會幫助其他人,所以我會將其標記爲正確。謝謝 –

0

根據您的意見,這聽起來像你想追加而不是。如果你想附加到一個文件(即添加到已有的文件),你需要使用AppendAllLines()函數。

但是,您有更大的問題,因爲您正在嘗試寫入對象的列表,而不是字符串到您的文件。如果你想只寫密碼,你需要這樣的:

private static void SaveToFile() 
{ 
    System.IO.File.AppendAllLines("path/to/file", myAccountList.Select(x => x.Password)); 
} 

此方法將追加到一個文件,如果它存在,如果不存在,它只是創建一個新的文件。

文檔here

+0

這不會編譯.. –

+0

@ S.Nog我更新了我的答案,我沒有注意到你正在試圖寫一個'IEnumerable '到一個文件。我認爲_your_代碼編譯。 – maccettura

+0

看起來這樣會創建.txt文件,但它實際上並沒有將'Password'字符串寫入文件。是的,從一開始我就不應該澄清這一點。 –

相關問題