WPF仿微信保存与查看聊天记录

本文介绍了如何在WPF应用中模仿微信保存和查看聊天记录。通过在服务器上创建用户和聊天伙伴的文件夹,存储聊天记录和图片。发送消息时,将文本内容转化为二进制写入txt文档;查看记录时,读取txt文档的二进制内容并还原为文本显示。
摘要由CSDN通过智能技术生成

WPF仿微信保存与查看聊天记录

之前做了仿微信发送消息,当然也要有个聊天记录来保存消息内容啦,下面一起来看看
ViewModel中在服务器中创建用户与当前聊天好友的文件夹,以便存放聊天记录文件和图片文件,代码如下:

/// <summary>
        /// 创建用户聊天文件
        /// </summary>
        /// <param name="msg"></param>
        public virtual void CreateUserChatFile(string msg)
        {
   
            CurrentFriend.UnreadMsg = false;
            string basedirectoryfolder = System.AppDomain.CurrentDomain.BaseDirectory + @"Files\";
            string userchatfile = basedirectoryfolder + currentUser.FriendName + "\\" + @"ChatLogs\";
            //判断文件夹是否存在
            if (!Directory.Exists(userchatfile))
            {
   
                //创建文件夹
                Directory.CreateDirectory(userchatfile);
            }
            string named = currentFriend.FriendName + currentFriend.FriendId;
            //文件命名
            string chatfile = EncryptionDecryption.MD5Encrypt(named, DataCenterManager.KEY_PASSWORD_STRING);
            //拼接路径
            string savefilepath = userchatfile + chatfile;
            //判断与该好友的聊天记录文件是否存在
            if (!File.Exists(savefilepath))
            {
   
                FileStream fs = new FileStream(savefilepath, FileMode.Create);
            }
        }

以下这段代码放在发送消息的方法中,主要就是将文本内容转换为二进制写入txt文档

string basedirectoryfolder = System.AppDomain.CurrentDomain.BaseDirectory + @"Files\";
                    string userchatfile = basedirectoryfolder + currentUser.FriendName + "\\" + @"ChatLogs\";
                    //判断文件夹是否存在
                    if (!Directory.Exists(userchatfile))
                    {
   
                        //创建文件夹
                        Directory.CreateDirectory(userchatfile);
                    }
                    string named = currentFriend.FriendName + currentFriend.FriendId;
                    //文件命名
                   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值