/// <summary> /// 写json信息 /// </summary> /// <param name="usrin">要写入的usr对象</param> /// <param name="path">json的路径</param> public static void JsonIn(usr usrin, string path) { if (usrin.name == "" || usrin.pass == "") { DialogResult a = MessageBox.Show("未输入用户名或密码\n如果有保存的登录信息将清空!" , "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if ((int)a == 2) { return; } } try { DialogResult b = MessageBox.Show("保存的登录信息将更新,旧登录信息将删除!" , "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if ((int)b == 2) { return; } //参考 http://blog.csdn.net/gf771115/article/details/27114257 序列化 StreamWriter sw = new StreamWriter(path, false); string strSerializeJSON = JsonConvert.SerializeObject(usrin); sw.Flush(); sw.Write(strSerializeJSON); sw.Close(); } catch (FileNotFoundException) { FileStream myFs = new FileStream(path, FileMode.Create); byte[] init = System.Text.Encoding.ASCII.GetBytes("{\"name\":\"\",\"pass\":\"\"}"); myFs.Write(init, 0, init.Length); MessageBox.Show("登录信息不存在,已新建!"); myFs.Close(); } }
usr usrin = new usr(); //New一个usr对象,写json用 /// <summary> /// 读json信息 /// </summary> /// <param name="path">json文件的路径</param> /// <returns>usr类对象</returns> public static usr JsonOut(string path) { usr readback = new usr(); try { StreamReader sr = new StreamReader(path); string jsonText = sr.ReadLine(); //参考 http://blog.csdn.net/ximen250/article/details/9061851 JObject jo = JObject.Parse(jsonText); JToken ageTokenname = jo["name"]; readback.name = ageTokenname.ToString(); JToken ageTokenpass = jo["pass"]; readback.pass = ageTokenpass.ToString(); sr.Close(); } catch (FileNotFoundException) { FileStream myFs = new FileStream(path, FileMode.Create); byte[] init = System.Text.Encoding.ASCII.GetBytes("{\"name\":\"\",\"pass\":\"\"}"); myFs.Write(init, 0, init.Length); MessageBox.Show("登录信息不存在,已新建!"); myFs.Close(); return(readback); } return(readback); }
private void btn_auto_Click(object sender, EventArgs e) //快速登录 { usr user = new usr(); user = info.JsonOut(@"usr\\usr.json"); textBox_usernum.Text = encrypt.Base64Decode(encrypt.wvc(user.name)); textBox_pwd.Text = encrypt.Base64Decode(encrypt.wvc(user.pass)); btn_login_Click(btn_login, new EventArgs()); }
private void btn_quickLog_Click(object sender, EventArgs e) //保存登录 { MessageBox.Show("将保存现在学号密码栏中的登录信息以便下次登录"); usr user = new usr(); user.name = encrypt.wvc(encrypt.Base64Encode(textBox_usernum.Text)); user.pass = encrypt.wvc(encrypt.Base64Encode(textBox_pwd.Text)); info.JsonIn(user, @"usr\\usr.json"); LogWriter.write(textBox_usernum.Text + "保存登录信息"); }