using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace 文件操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ReadJson();
}
string filepath = @"D:\Users\任伟林\文档\\c#\01-my first\文件操作\test.txt";
private void btn_read_Click(object sender, EventArgs e)
{
FileStream fileStream = new FileStream(filepath, FileMode.Open);
StreamReader streamReader = new StreamReader(fileStream);
string line = streamReader.ReadToEnd();
txb_read.Text = line.ToString();
fileStream.Close();
}
private void btn_write_Click(object sender, EventArgs e)
{
FileStream fileStream2 = new FileStream(filepath, FileMode.Append);
StreamWriter streamWriter = new StreamWriter(fileStream2);
streamWriter.WriteLine(txb_write.Text);
streamWriter.Flush();
streamWriter.Close();
fileStream2.Close();
MessageBox.Show("写入成功");
}
/// <summary>
/// setting 操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_set_Click(object sender, EventArgs e)
{
Properties.Settings.Default.aaa = txb_set.Text;
Properties.Settings.Default.Save();
}
private void btn_readset_Click(object sender, EventArgs e)
{
txb_readset.Text = Properties.Settings.Default.aaa;
}
/// <summary>
/// json记录账号密码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UserLogin_Load(object sender, EventArgs e)
{
ReadJson();//读取配置文件
}
private void ReadJson()
{//读取配置文件
string jsonPath = Application.StartupPath + @"\userInfo.json";
if (File.Exists(jsonPath))
{
try
{
string jsonStr = File.ReadAllText(jsonPath);
JObject jsonObj = JObject.Parse(jsonStr);
if (jsonObj != null)
{//读取用户信息
if (jsonObj["id"] != null)
{
switch (jsonObj["id"].ToString())
{//是否记住用户信息
case "0"://不记住登陆信息
RdbNotSave.Checked = true;
break;
case "1"://记住账号
RdbSavename.Checked = true;
break;
case "2"://记住账号和密码
RdbSaveBoth.Checked = true;
break;
default:
RdbNotSave.Checked = false;
RdbSaveBoth.Checked = false;
RdbSaveBoth.Checked = false;
break;
}
}
if (jsonObj["userName"] != null)
{
txb_user.Text = jsonObj["userName"].ToString();
}
if (jsonObj["passWord"] != null)
{
txb_psw.Text = jsonObj["passWord"].ToString();
}
}
}
catch
{
}
}
}
private void WriteJson(string id, string userName, string passWord)
{//写入配置文件
string jsonPath = Application.StartupPath + @"\userInfo.json";
string jsonStr = "{\"id\":\"" + id + "\",\"userName\":\"" + userName + "\",\"passWord\":\"" + passWord + "\"}";
File.WriteAllText(jsonPath, jsonStr);
Console.WriteLine(jsonStr);// 显示{"id":"2","userName":"rwl321","passWord":"123456"}
}
private void btn_login_Click(object sender, EventArgs e)
{
{
//判断记录状态
if (RdbNotSave.Checked)
{//不记住登陆信息
WriteJson("0", "", "");
}
else if (RdbSavename.Checked)
{//记住账号
WriteJson("1", txb_user.Text.Trim(), "");
}
else if (RdbSaveBoth.Checked)
{//记住账号和密码
WriteJson("2", txb_user.Text.Trim(), txb_psw.Text.Trim());
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//UserLogin_Load();
//ReadJson();
}
private void button1_Click(object sender, EventArgs e)
{
Environment.Exit(0);//退出软件
}
}
}