2013-09-30 35 views
-1

我想確定用戶在註冊時是否單擊管理員或用戶單選按鈕。我想我應該將它追加到文本文件的名稱和密碼所在行的末尾,但我該怎麼做?下面是相關代碼:從布爾方法傳遞新的值字符串?

單選按鈕檢查

public bool radioButtons() 
{ 
    string usertypebutton; 
    if (!userButton.Checked && !adminButton.Checked) 
    { 
     MessageBox.Show("You must select an account type"); 
     return false; 
    } 
    else 
    { 
     if (userButton.Checked) 
     { 
      usertypebutton = "User"; 
     } 
     else 
     { 
      usertypebutton = "Admin"; 
     } 
     return true; 

    } 
} 

的StreamWriter進行註冊:

public void mySW() 
{ 
    string path = @"C:\Other\myFile.txt"; 
    string userName = userNameBox.Text; 
    string password = passwordBox.Text; 
    string usertype = usertypebutton; 

    using (StreamWriter writer = new StreamWriter(path, true)) 
    { 
     writer.WriteLine("Username: {0} Password: {1} Type: {3}" , userName, password, usertype); 

     // No need to close nor dispose your StreamWriter. 
     // You're inside a using statement for that! 
    } 

    MessageBox.Show("Thanks for registering! \n\nYou may now log in!", "Registration SuccessFul"); 
    Application.OpenForms[0].Show(); 
    this.Close(); 
} 

登錄:

private void logonButton_Click(object sender, EventArgs e) 
{ 
    // Loads your users storage 
    var users = File.ReadAllLines(@"C:\Other\myFile.txt"); 

    // Creates the line with username + password 
    var usernamePassword = String.Format("Username: {0} Password: {1}", userNameBox.Text, passwordBox.Text); 

    // Locates the user on your storage 
    var userFound = users.SingleOrDefault(_u => _u.Equals(usernamePassword)); 

    if (userFound != null) 
    { 
     MessageBox.Show("Welcome back, " + userNameBox.Text); 
    } 
    else 
    { 
     MessageBox.Show("Sorry, you have entered incorrect details\n\nPlease try again"); 
     userNameBox.Text = ""; 
     passwordBox.Text = ""; 
    } 
} 

所以(我認爲)基本上我想將單選按鈕方法的值usertypebutton傳遞給SW。我將如何做,因爲我已經傳遞了一個布爾值?

安東尼

+3

您提到您在您的帖子中整理了以前的問題。 1)這個問題無關緊要,而且浪費時間閱讀。 2)你從未真正接受過上一個問題的答案。如果你有答案,那麼你應該將其標記爲已接受。 –

+0

sorted :)謝謝 –

+0

所以任何人都可以幫忙嗎? –

回答

0

我將返回類型更改爲字符串,並返回字符串「ERROR」如果選擇任何複選框。在mySW()中,我將檢查在打開StreamWriter之前是否返回了「ERROR」,並且如果它確實退出該函數,則不會執行任何操作。

0

不要在任何地方寫入未加密的純文本密碼。這是可怕的不安全,如果你曾經被黑客攻擊,可能會給你帶來很多麻煩。在其他任何事情之前,你真的需要尋找你信任的密碼加密策略。

至於你的實際問題,你已經有了:

writer.WriteLine("Username: {0} Password: {1} Type: {3}" , userName, password, usertype); 

只要改變它採取在選定的單選按鈕的字符串值,你可以寫出來管理員或使用者。