-1

これが私のコードです。これは非常に基本的なログインシステム用で、ユーザー名とパスワードをテキストファイルに書き込み、別のフォームのログイン画面で比較します。これは、登録ユーザー コードです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
    private void Form2_Load(object sender, EventArgs e)
    {
        pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
    }

    public Form2()
    {
        InitializeComponent();
    }
    public bool radioButtons()
    {
        if (!userRadioButton.Checked && !adminRadioButton.Checked)
        {
            MessageBox.Show("You must select an account type");
            return false;
        }
        else
        {
            return true;
        }
    }

    public void button1_Click(object sender, EventArgs e)
    {
        bool a = radioButtons();
        if (a == true)
        {
            string userName = userNameBox.Text;
            string password = passwordBox.Text;
            var userNames = File.ReadAllLines(@"C:\Other\myFile.txt");
            if (checkUsernameValid() && checkUsernameNotExist() && checkPasswordsValid() && checkPasswordsMatch())
            {
                allOK();
            }


        }   
    } 
    public void mySW()
    {
         string path = @"C:\Other\myFile.txt";
        string userName = userNameBox.Text;
        string password = passwordBox.Text;
        using (StreamWriter writer = new StreamWriter(path, true))
        {
            writer.WriteLine("Username and Password: {0} {1}",userName,password);
            writer.WriteLine();
            writer.Close();
            writer.Dispose();
        }
        MessageBox.Show("Thanks for registering! \n\nYou may now log in!","Registration SuccessFul");
        Application.OpenForms[0].Show();
        this.Close();
    }
    public bool checkUsernameValid()
    {
        if (userNameBox.Text == "")
        {
            MessageBox.Show("Username cannot be empty", "Invalid Username Entry");
            return false;
        }
        else
            return true;
    }
    public bool checkPasswordsMatch()
    {
        if (!passwordBox.Text.Equals(repeatPasswordBox.Text))
        {
            MessageBox.Show("Sorry, your passwords do not match, try again", "Password Error");
            passwordBox.Text = "";
            repeatPasswordBox.Text = "";
            return false;
        }
        else
            return true;
    }
    public bool checkUsernameNotExist()
    {
        if (userNameBox.Text.Contains("Username: " + userNameBox.Text))
        {
            MessageBox.Show("Sorry, that user name is not available, try again", "Invalid Username Entry");
            userNameBox.Text = "";
            passwordBox.Text = "";
            repeatPasswordBox.Text = "";
            return false;
        }
        else
            return true;
    }
    public void allOK()
    {
        if (!userNameBox.Text.Contains("Username: " + userNameBox.Text) && passwordBox.Text == repeatPasswordBox.Text)
            {
                mySW();
            }
    }
    public bool checkPasswordsValid()
    {
        if (passwordBox.Text == "")
        {
            MessageBox.Show("Password fields cannot be empty", "Password Error");
            return false;
        }
        else
            return true;
    }

   }
}

ユーザー名を入力すると、すべてのチェックと登録が行われますが、ユーザー名が既に存在する場合は登録できますか???

4

2 に答える 2

1
if (userNameBox.Text.Contains("Username: " + userNameBox.Text))

この行は混乱しています。(この行はいつでも同じ結果を与えますTrue

しかし、私はあなたに1つのアイデアを与えます

. 登録された値をデータベースに保存します。次に、ユーザーが 2 回目に同じユーザー名を指定します。次に、SQL クエリを使用してユーザー名が既に存在することを確認します。

この前の議論を参照してください: データベース内にユーザー名が既に存在するかどうかを確認する

于 2013-09-29T13:02:31.673 に答える
0

「userlist」という名前のリッチテキスト ボックスを挿入します。visible を false に設定し、最初にクリアしてください。

 public bool checkUsernameNotExist()
    {
        string readfile = System.IO.File.ReadAllText(@"C:\file.txt");
        userlist.Text = readfile;
        for (int i = 0; i < userlist.Lines.Length; i++)
        {
            string line = userlist.Lines[i];
            if (line.Contains("Username: " + userNameBox.Text))
            {
                MessageBox.Show("Sorry, that user name is not available, try again", "Invalid Username Entry");
                userNameBox.Text = "";
                passwordBox.Text = "";
                repeatPasswordBox.Text = "";
                return false;
            }
            else
                return true;
        }

これを行うことで、ユーザー名を暗号化/復号化できます。

void encrypt()
{
string encrypted = "";
string encryptstr = yourtextBox.Text;
int encryptnum = 15; //The bigger the number, the stronger the encryption
char[] toencrypt = encryptstr.ToCharArray();
for (int i = 0; i < toencrypt.Length; i++)
int num = Convert.ToInt32(toencrypt[i]) + encryptnum;
string output = Convert.ToChar(num).ToString();
encrypted += output;
}

void decrypt() 
{
string decrypted = "";
string decryptstr = yourtextBox.Text;
int decryptnum = 15; //This must be the same as you used to encrypt
char[] todecrypt = decryptstr.ToCharArray();
for (int i = 0; i < todecrypt.Length; i++)
int num = Convert.ToInt32(todecrypt[i]) - decryptnum;
string output = Convert.ToChar(num).ToString();
decrypted += output;
}

お役に立てれば

于 2013-09-29T15:34:30.113 に答える