文件操作 test

该代码示例展示了如何在C#的WindowsForms应用中进行文件读写,包括读取文本文件、追加写入文本文件。此外,还使用Newtonsoft.Json库处理JSON格式的配置文件,用于存储和读取用户登录时的记住密码设置。用户可以选择记住用户名、密码或两者都不记住。
摘要由CSDN通过智能技术生成
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);//退出软件
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值