winform登陆记住密码

来源:互联网 发布:青岛海关数据 编辑:程序博客网 时间:2024/05/15 23:51

[Serializable]
    public class User
    {
        private string username;
        private string userpwd;
        private string visausername;
        private string visauserpwd;

        /// <summary>
        ///
        /// </summary>
        public string UserName
        {
            get { return username; }
            set { username = value; }
        }
        /// <summary>
        ///
        /// </summary>
        public string UserPwd
        {
            get { return userpwd; }
            set { userpwd = value; }
        }
        /// <summary>
        ///
        /// </summary>
        public string VisaUserName
        {
            get { return visausername; }
            set { visausername = value; }
        }
        /// <summary>
        ///
        /// </summary>
        public string VisaUserPwd
        {
            get { return visauserpwd; }
            set { visauserpwd = value; }
        }
    }

 

 


  Dictionary<string, User> users = new Dictionary<string, User>();

 

//保存User对象       
        public void SetDatCatch(string FolderName, string DatCatchName)
        {
            //获取绝对目录
            string sURL = System.Web.Hosting.HostingEnvironment.MapPath("\\") + @"Cache\\" + FolderName;

            //确定是否有文件目录,没有就添加
            if (!System.IO.Directory.Exists(sURL))
            {
                System.IO.DirectoryInfo di = System.IO.Directory.CreateDirectory(sURL);
            }
            string sMapPath = sURL + "\\" + DatCatchName + ".dat";

            Stream fStream = new FileStream(sMapPath, FileMode.Create, FileAccess.ReadWrite);

            BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器

            binFormat.Serialize(fStream, users);

            fStream.Close();
        }

 

public void GetDatCatch(string FolderName, string DatCatchName)
        {
            User user = new User();
            //获取绝对目录
            string sURL = System.Web.Hosting.HostingEnvironment.MapPath("\\") + @"Cache\\" + FolderName;

            string sMapPath = sURL + "\\" + DatCatchName + ".dat";

            if (File.Exists(sMapPath))
            {
                //使用二进制反序列化对象
                Stream fStream = new FileStream(sMapPath, FileMode.Open);
                BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
                if (fStream.Length > 0)
                {
                    users = binFormat.Deserialize(fStream) as Dictionary<string, User>;
                    //UserInfo = (User)binFormat.Deserialize(fStream);//反序列化对象
                    if (users.Count > 0)
                    {
                        int count = users.Count;
                        user = users.Values.Last();
                        if (!string.IsNullOrEmpty(user.UserName))
                        {
                            this.txtUserName.Text = user.UserName;
                            this.chkJZERPName.Checked = true;
                            if (this.txtUserName.Text != "")
                            {
                                this.txtUserPwd.Text = user.UserPwd;
                                this.chkJZERPwd.Checked = true;
                            }
                        }
                        if (!string.IsNullOrEmpty(user.VisaUserName))
                        {
                            this.txtVisaUser.Text = user.VisaUserName;
                            this.chkJZVisaName.Checked = true;

                            if (this.txtVisaUser.Text != "")
                            {
                                this.txtVisaPwd.Text = user.VisaUserPwd;
                                this.chkJZVisaPwd.Checked = true;
                            }
                        }

                    }
                }
                fStream.Close();
            }

        }

 

0 0
原创粉丝点击