Exemplo n.º 1
0
        private Dictionary <string, UsersInfo> OpenCsv(Dictionary <string, UsersInfo> dictionaryOpen, string path)
        {
            //StudentsInfo
            Encoding encoding = GetType(path); //Encoding.ASCII;//

            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open,
                                                               System.IO.FileAccess.Read);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, encoding);

            //记录每次读取的一行记录
            string strLine = "";

            //记录每行记录中的各字段内容
            string[] aryLine = null;
            //tableHead = null;


            //标示是否是读取的第一行
            bool IsFirst = true;

            //逐行读取CSV中的数据,直到结尾
            while ((strLine = sr.ReadLine()) != null)
            {
                if (IsFirst == true)
                {
                    //tableHead = strLine;
                    IsFirst = false;
                }
                else
                {
                    aryLine = strLine.Split(',');
                    UsersInfo usersInformationclass = new UsersInfo();

                    usersInformationclass.NameUsers = aryLine[0];
                    usersInformationclass.PswUsers  = aryLine[1];
                    try
                    {
                        usersInfo.Add(usersInformationclass.NameUsers, usersInformationclass);
                    }
                    catch (ArgumentException)
                    {
                        MessageBox.Show(this, "添加失败!"
                                        + "用户" + usersInformationclass.NameUsers
                                        + "已存在!", "提示", MessageBoxButtons.OK);
                    }
                }
            }

            sr.Close();
            fs.Close();

            return(dictionaryOpen);
        }
Exemplo n.º 2
0
        private void button_register_Click(object sender, EventArgs e)
        {
            UsersInfo usersInformationclass = new UsersInfo();

            usersInformationclass.NameUsers   = textBox_user.Text;
            usersInformationclass.PswUsers    = textBox_psw.Text;
            usersInformationclass.Level1Score = 0;
            usersInformationclass.Level2Score = 0;
            usersInformationclass.Level3Score = 0;


            if ((usersInformationclass.NameUsers == "") || (usersInformationclass.NameUsers == ""))
            {
                MessageBox.Show(this, "用户名或密码不能为空!", "提示", MessageBoxButtons.OK);
            }
            else
            {
                //利用字典添加
                try
                {
                    usersInfo.Add(textBox_user.Text, usersInformationclass);
                    MessageBox.Show(this, "注册成功!", "提示", MessageBoxButtons.OK);
                }
                catch (ArgumentException)
                {
                    MessageBox.Show(this, "注册失败!" + "用户" + textBox_user.Text + "已存在!", "提示", MessageBoxButtons.OK);
                }
            }

            FileInfo fi = new FileInfo(Register_Login_MainForm._filePath);

            if (!fi.Directory.Exists)
            {
                fi.Directory.Create();
            }

            FileStream   fs = new FileStream(Register_Login_MainForm._filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
            //输出字段名
            string data = "";

            data = "NameUsers,PswUsers,Level1Score,Level2Score,Level3Score";
            sw.WriteLine(data);
            //写出各行数据
            foreach (KeyValuePair <string, UsersInfo> kvp in Register_Login_MainForm.usersInfo)
            {
                //Console.WriteLine("Key = {0}, Value = {1}",
                //    kvp.Key, kvp.Value);
                //kvp.Value.Level1Score = 0;
                //kvp.Value.Level2Score = 0;
                //kvp.Value.Level3Score = 0;

                data = kvp.Value.NameUsers + "," +
                       kvp.Value.PswUsers + "," +
                       kvp.Value.Level1Score + "," +
                       kvp.Value.Level2Score + "," +
                       kvp.Value.Level3Score;
                sw.WriteLine(data);
            }
            sw.Close();
            fs.Close();
        }