'C# 아이디 저장'에 해당되는 글 1건

728x90



로그인 ID를 매번 입력해야 하는 번거로움을 없애는 방법이 뭘까?


Registry 에 정보를 저장하고 읽어오는 방법도 있는거 같은데 정보를 저장할때 관리자 권한으로 실행을 해야 저장이 가능한거 같다.


현재 상태의 설정값(로그인 ID, 비밀번호 등)을 저장하여 다음번 Load 시 불러다 이용하려고 할 경우

Properties.Settings 를 이용한다.


먼저, 솔루션 탐색기에서 Properties 를 선택하고 Settings.settings 를 더블 클릭한다.




이름 적는 곳에 기록할 이름을 적어준다. 그리고 필요하면 값부분에 적어준다.



이제 Load 될 때와 Form 이 Closing 될 때 코드를 추가해준다.


private void frmLogin_Load(object sender, EventArgs e)
{
    txtID.Text = Properties.Settings.Default.LoginIDSave;
}

private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!CK)
    {
        Application.Exit();
    }
    else
    {
        Properties.Settings.Default.LoginIDSave = txtID.Text;
        Properties.Settings.Default.Save();
    }
}


최초 로그인할 때는 아이디 입력창이 빈 상태이겠지만 프로그램을 한번 실행하고 나서 다음에 로그인하면 아래 그림처럼 ID 정보가 자동으로 불러와 있다.



이 정보는 C:\Users\사용자\AppData 하위의 해당 App 폴더에 값이 저장된다.




아이디 저장을 체크한 경우에 저장되도록 하고자 한다면,  Form_Closing 되는 곳에서는 삭제하고 로그인 OK 되는 이벤트에서 값을 저장해준다.


private void btnOK_Click(object sender, EventArgs e)
{
    if (this.txtID.Text == "")
    {
        MessageBox.Show("로그인 아이디를 입력하세요", "알림", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        this.txtID.Focus();
    }
    else if (this.txtPwd.Text == "")
    {
        MessageBox.Show("로그인 비밀번호를 입력하세요", "알림", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        this.txtPwd.Focus();
    }
    else
    {
        if (IDSave.Checked) // 로그인 OK 버튼 실행할 때 저장
        {
            Properties.Settings.Default.LoginIDSave = txtID.Text;
            Properties.Settings.Default.Save();
        }
        mySqlLogin();
    }
}



아래 코드는 윈도우 운영체제 부팅하면서 입력창 나오는 곳에서 입력한 ID 정보를 얻어내는 방법이다.


string userName = null;
using (System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent())
{
    userName = wi.Name;
}


MessageBox.Show(userName);


블로그 이미지

Link2Me

,