using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary; namespace xtragrid
{
public partial class Form1 : Form
{
ArrayList al = new ArrayList(); //用此存储用户名 当然还想存储密码就用Dictionary<>
public Form1()
{
InitializeComponent();
}
//窗体load 读取本地user.data文件,获取用户
private void Form1_Load(object sender, EventArgs e)
{
FileStream fs = new FileStream("user.data",FileMode.OpenOrCreate);//打开user.data,若不存在就建立新的
if (fs.Length>)
{
BinaryFormatter bf = new BinaryFormatter();
al = bf.Deserialize(fs) as ArrayList; //将打开的文件流反序列化到al
foreach (string item in al)
{
this.comboBox1.Items.Add(item); //将al各项添加到combobox中
}
}
fs.Close(); //关闭流
this.comboBox1.SelectedIndex = this.comboBox1.Items.Count - ; //设置combobox默认项
}
//登陆按钮
private void button1_Click(object sender, EventArgs e)
{
string name = this.comboBox1.Text;
//check name
if (true) //存在此用户
{
if (this.checkBox1.Checked) //勾选记住用户
{ if (!al.Contains(name)) //如果al中没有这个用户
{
FileStream fs = new FileStream("user.data", FileMode.Create); //创建新文件user.data
BinaryFormatter bf = new BinaryFormatter(); //创建二进制序列化对象
al.Add(name);
bf.Serialize(fs, al); //序列化到fs
fs.Close(); //关闭文件流
}
}
//login code
}
else
{
//no user
} }
}
}