2016-12-02 56 views
0

我在C#中創建一個登錄應用程序,並且我想將電子郵件,用戶名和密碼保存在一個.txt文件中,我怎樣才能做到這一點,然後在IM登錄時加載用戶名和密碼在主窗口中的電子郵件?從.txt保存和加載數據文件

這是我的登錄代碼:

string dir = textBox1.Text; 
      if (!Directory.Exists("data\\" + dir)) 
       MessageBox.Show("User Dosen't Exist!", dir); 
      else 
      { 
       var sr = new StreamReader("data\\" + dir + "\\data.ls"); 

       string encusr = sr.ReadLine(); 
       string encpass = sr.ReadLine(); 
       string encemail = sr.ReadLine(); 
       sr.Close(); 

       string decusr = Encryption.Encrypt.decrypt(encusr); 
       string decpass = Encryption.Encrypt.decrypt(encpass); 
       string decemail = Encryption.Encrypt.decrypt(encemail); 

       if (decusr == textBox1.Text && decpass == textBox2.Text) 
       { 

        MessageBox.Show("Welcome To Private Area", decusr); 
        Main_Form frm = new Main_Form(); 
        frm.Show(); 
        this.Hide(); 
       } 
       else 
       { 
        MessageBox.Show("Password Or Username Is Wrong!", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
      } 

,這將節省代碼:

string dir = textBox1.Text; 
      Directory.CreateDirectory("Data\\" + dir); 

      var sw = new StreamWriter("Data\\" + dir + "\\data.ls"); 

      string encusr = Encryption.Encrypt.encrypt(textBox1.Text); 
      string encpass = Encryption.Encrypt.encrypt(textBox2.Text); 
      string encemail = Encryption.Encrypt.encrypt(textBox3.Text); 

      sw.WriteLine(encusr); 
      sw.WriteLine(encpass); 
      sw.WriteLine(encemail); 
      sw.Close(); 

      MessageBox.Show("User Was Successfully Created: " + textBox1.Text); 
      this.Close(); 
+0

你è在執行過程中遇到任何錯誤? –

+0

國際海事組織你應該使它像CSV樣式(「用戶名」;「密碼」;「電子郵件」),然後你可以逐行閱讀這個文件,並檢查是否(line [0] == usernameInput) –

+0

當然,你是去尋找修復它的方法,但我認爲,即使你找到了一個解決方案,你也會走錯方向。 txt文件不是存儲該信息的最佳方式。 嘗試查找XML文件或JSON文件,如果您想深入瞭解,爲什麼不嘗試使用SQLLite? –

回答

0

你可以改變Main_Form包括像這樣的電子郵件字段:

public class Main_Form : Window 
{ 
    public string Email { get; set; } 
    // skipped rest of main form implementation 
} 

然後,您可以在創建Main_Form時設置電子郵件字段:

MessageBox.Show("Welcome To Private Area", decusr); Main_Form frm = new Main_Form(); frm.Email = decemail; frm.Show(); this.Hide();

0

的所有代碼首先將只是看裏面的文件的第一個條目,這個你應該做類似修正:

using (var sr = new StreamReader("data\\" + dir + "\\data.ls")){ 
    string line = string.Empty; 
    while((line = sr.ReadLine()) != null) { 
     // assume that 1 is username 
     if(decusr == textBox1.Text) { 
      if((line = sr.ReadLine()) == decpass == textBox2.Text) { 
       // user is logged in store email 
       UserEmail = sr.ReadLine(); 
      } 
     } 
    } 
} 

這樣做,你可以只添加成員字段後財產到您的Main_Form像這樣:

public class Main_Form : Window 
{ 
    public string UserEmail { get; private set; } 
    /* 
    * Rest of your code 
    */ 
} 

編輯:(T他更好的辦法,但仍然使用txt文件)

更好的辦法是隻重寫以CSV存儲數據,如文件的保存/加載邏輯:

public string ProduceLine(string username, string password, string email){ 
    return string.Join(";", new string[] { 
     Encryption.Encrypt.encrypt(username), 
     Encryption.Encrypt.encrypt(password), 
     Encryption.Encrypt.encrypt(email) 
    }; // this will return something like "encrypted_username;encrypted_password;encrypted_email" 
} 

然後,當你將加載該文件,你可以只用簡單的方法來比較:

public string TryLogin(string username, string password) { 
    // this will return user email upon succesfull logging in 
    using (var sr = new StreamReader("data\\" + dir + "\\data.ls")){ 
     string line = string.Empty; 
     while((line = sr.ReadLine()) != null) { 
      string[] extracted = string.Split(";"); 
      if(extracted[0] == Encryption.Encrypt.encrypt(username) && extracted[1] == Encryption.Encrypt.encrypt(password)) { 
       return Encryption.Encrypt.decrypt(extracted[1]); 
      } 
     } 
    } 
    return string.Empty; 
} 

這將允許你到你的方法減磅,如:

string email = TryLogin(textBox1.Text, textBox2.Text); 
if(!string.IsNullOrEmpty(email)) 
{ 
    MessageBox.Show("Welcome To Private Area", decusr); 
    Main_Form frm = new Main_Form(); 
    frm.Show(); 
    this.Hide(); 
} 
else 
{ 
    MessageBox.Show("Password Or Username Is Wrong!", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 
}  
+0

string [] extracted = string.Split(「;」);給我一個錯誤 –