winform登陆 记住用户名 代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.Collections;
10 using System.IO;
11 using System.Runtime.Serialization.Formatters.Binary;
12 
13 namespace xtragrid
14 {
15     public partial class Form1 : Form
16     {
17         ArrayList al = new ArrayList(); //用此存储用户名 当然还想存储密码就用Dictionary<>
18         public Form1()
19         {
20             InitializeComponent();
21         }
22          //窗体load 读取本地user.data文件,获取用户
23         private void Form1_Load(object sender, EventArgs e)
24         {
25             FileStream fs = new FileStream("user.data",FileMode.OpenOrCreate);//打开user.data,若不存在就建立新的
26             if (fs.Length>0) 
27             {
28                 BinaryFormatter bf = new BinaryFormatter();
29                 al = bf.Deserialize(fs) as ArrayList; //将打开的文件流反序列化到al
30                 foreach (string item in al)
31                 {
32                     this.comboBox1.Items.Add(item); //将al各项添加到combobox中
33                 }
34             }
35             fs.Close();  //关闭流
36             this.comboBox1.SelectedIndex = this.comboBox1.Items.Count - 1; //设置combobox默认项
37         }
38         //登陆按钮
39         private void button1_Click(object sender, EventArgs e)
40         {
41             string name = this.comboBox1.Text;
42             //check name
43             if (true) //存在此用户
44             {
45                 if (this.checkBox1.Checked)  //勾选记住用户
46                 {
47                     
48                     if (!al.Contains(name)) //如果al中没有这个用户
49                     {
50                         FileStream fs = new FileStream("user.data", FileMode.Create); //创建新文件user.data
51                         BinaryFormatter bf = new BinaryFormatter(); //创建二进制序列化对象
52                         al.Add(name);
53                         bf.Serialize(fs, al); //序列化到fs
54                         fs.Close();           //关闭文件流
55                     }
56                 } 
57                 //login code
58             }
59             else
60             {
61                 //no user
62             } 
63            
64 
65         }
66     }
67 }

 

posted on 2013-07-05 17:43  MisterS  阅读(913)  评论(1编辑  收藏  举报

导航