栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

基于WinForm客户端的简单本地缓存实战

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于WinForm客户端的简单本地缓存实战

简而言之:WinForm是·Net开发平台中对Windows Form的一种称谓,开发语言通常是C#,一款微软出品并可以媲美Java的面向对象编程语言,它含有一个窗体,作为控件和其它图形的平台,可以开发智能客户端,比如QQ、Photoshop等C端,在Windows平台下非常NICE,笔者在开发一款【车辆管理系统】软件中,用到了缓存机制,简单记录一下。

一,效果图,是否如你所想,点击配置代理,弹出需要存储的Neek和AppKey,这两个值即是需要缓存到本地的,以便下次启动项目时使用此值。

二,创建一个普通类:AgencyCache,编写缓存和读取代码。

1、缓存代码:

  public static void Cache(String neek, String appkey)
        {
           
            Dictionary keys = new Dictionary();

            FileStream fileStream = new FileStream("agency.data", FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            keys.Add("neek", neek);
            keys.Add("appkey",appkey);

            binaryFormatter.Serialize(fileStream, keys);

            fileStream.Close();
        }

2、读取代码:

    public static Dictionary Query()
        {
            Dictionary keys = new Dictionary();

            FileStream fileStream = new FileStream("agency.data", FileMode.OpenOrCreate);
            if (fileStream.Length > 0)
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();

                keys = binaryFormatter.Deserialize(fileStream) as Dictionary;

                Console.WriteLine("缓存数据:" + "=Neek=" + keys["neek"] + "=AppKey=" + keys["appkey"]);
            }
            else
            {
                keys.Add("neek", "");
                keys.Add("appkey", "");
            }

            fileStream.Close();

            return keys;
        }

3、如何使用代码,即当点击配置代理时打开弹窗并读取数据值显示,当点击保存配置时则缓存本地。

private void AgencyForm_Load(object sender, EventArgs e)
        {

            Console.WriteLine("开始载入窗体,读取保存数据");


            this.NeekTextBox.Text = AgencyCache.Query()["neek"];
            this.AppKeyTextBox.Text = AgencyCache.Query()["appkey"];


            this.NeekTextBox.Select(this.NeekTextBox.TextLength, 0);


        }
 private void SaveBtn_Click(object sender, EventArgs e)
        {

            if (this.NeekTextBox.Text.Length == 0 || this.AppKeyTextBox.Text.Length == 0)
            {
                MessageBox.Show("抱歉,配置代理不能为空");
            }
            else
            {
                AgencyCache.Cache(this.NeekTextBox.Text, this.AppKeyTextBox.Text);

                MessageBox.Show("保存配置成功");
            }
        }

三、查看创建的缓存文件位置在项目根目录,如下图:

至此,一个非常简单又实用的基于WinForm客户端的本地数据缓存功能就实现了,春暖花开,我还在写代码。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906214.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号