c# txt文档的实时显示,用来查看发送接收指令

devtools/2025/3/14 5:54:43/

在这里插入图片描述
通讯历史按钮

        private void uiButton1_Click(object sender, EventArgs e){try{logf = new logF();logf.Show();}catch (Exception){throw;}     }

主页面关闭函数(点击保存就为true true就不删除)

        private void page1_FormClosed(object sender, FormClosedEventArgs e){// 关闭应用时删除txt文件 为true就不删除if (logF.savelog== false){if (File.Exists(logF.filePath)){File.Delete(logF.filePath);}}Application.Exit();}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;namespace WindowsFormsApp1
{public partial class logF : UIForm{public logF(){InitializeComponent();Application.EnableVisualStyles();//Application.Run(new MainForm());MainForm();}private FileSystemWatcher fileWatcher;public static string filePath = @"C:\log\run.txt";public static bool savelog=false;public void MainForm(){ 初始化RichTextBox//richTextBox1 = new RichTextBox();//richTextBox1.Dock = DockStyle.Fill;//this.Controls.Add(richTextBox1);// 初始化FileSystemWatcherfileWatcher = new FileSystemWatcher();fileWatcher.Path = Path.GetDirectoryName(filePath);fileWatcher.Filter = Path.GetFileName(filePath);fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;// 绑定事件处理程序fileWatcher.Changed += OnFileChanged;fileWatcher.EnableRaisingEvents = true;// 初始加载文件内容LoadFileContent();}private void LoadFileContent(){try{// 读取文件内容并显示在RichTextBox中using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))using (StreamReader reader = new StreamReader(fs)){richTextBox1.Text = reader.ReadToEnd();}}catch (Exception ex){MessageBox.Show("Error reading file: " + ex.Message);}}private void OnFileChanged(object sender, FileSystemEventArgs e){在UI线程上更新RichTextBox内容//this.Invoke((MethodInvoker)delegate//{//    LoadFileContent();//});if (this.IsHandleCreated) // 确保句柄已存在{this.BeginInvoke((MethodInvoker)delegate{LoadFileContent();});}}private void logF_Load(object sender, EventArgs e){ 读取txt文件内容并显示在文本框中//if (File.Exists(filePath))//{//    string content = File.ReadAllText(filePath);//    richTextBox1.Text = content;//}//else//{//    MessageBox.Show("文件不存在!");//}}private void uiButton7_Click(object sender, EventArgs e){savelog = true;}private void uiButton1_Click(object sender, EventArgs e){System.Diagnostics.Process.Start(@"C:\log");}}
}

log.cs
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WindowsFormsApp1
{internal class log{public static void SaveLog(string log, string filename = @"C:\log\run.txt"){string pathA = filename.Substring(0, filename.LastIndexOf("\\"));if (!Directory.Exists(pathA)){DirectoryInfo directoryInfo = new DirectoryInfo(pathA);directoryInfo.Create();}string filepathA = filename;try{String time = DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]");time += log;if (!File.Exists(filepathA)){using (StreamWriter sw = File.CreateText(filepathA)){sw.WriteLine(time);sw.Close();}}else{{using (StreamWriter sw = File.AppendText(filepathA)){sw.WriteLine(time);sw.Close();}}}}catch{// MessageBox.Show("log异常" + ee.ToString());}}}
}
log.SaveLog("要保存的内容");

http://www.ppmy.cn/devtools/166938.html

相关文章

**ResNet-SE + MFCC** 训练框架,包括 **数据加载、训练流程**,以及 **混淆矩阵** 可视化示例

1. 依赖库安装 如果你还没安装相关库,请先执行: pip install torch torchaudio torchvision scikit-learn matplotlib tqdm2. 数据加载 这里假设你有一个 音频分类数据集,其文件结构如下: dataset/ │── train/ │ ├──…

星越L_副驾驶屏使用讲解

目录 1.副驾驶屏在副驾驶前方 2.上方时间、网络、蓝牙显示 3.右侧导航可以返回主页,切换模式 4.空调控制 5.应用 6.应用商城下载应用 7.音乐 8.下滑屏幕 9.长按退出 10.一键息屏 1.副驾驶屏在副驾驶前方 此屏又叫娱乐屏,可以看电视,听音乐,终身免费会员,支持车机…

软件安全测评之渗透测试流程和工具分享

在信息技术快速发展的今天,软件安全显得尤为重要。软件渗透测试作为安全测评中重要的测试方法,是一种模拟攻击手段,用于识别应用程序、网络或系统中的安全漏洞。这一过程通常由专业的安全团队执行,目的是在攻击者可以利用这些漏洞…

Hutool RedisDS:Java开发中的Redis极简集成与高阶应用

在Java开发中,Redis作为高性能内存数据库,广泛应用于缓存、分布式锁等场景。然而原生的客户端操作涉及连接管理、序列化等繁琐细节。Hutool工具包提供的RedisDS模块,通过高度封装显著简化了这一过程。本文从实战角度解析其核心特性与使用技巧…

【机器学习】迁移学习(Transfer Learning)

迁移学习(Transfer Learning)作为一种机器学习方法,主要通过将源域中学到的知识迁移到目标域,解决目标域中数据不足或标注困难的问题,尤其在无监督学习如聚类任务中具有显著优势。迁移学习的关键思想包括领域适应、知识…

【DevOps】使用Azure DevOps为Azure静态网站配置多阶段部署

【DevOps】使用Azure DevOps为Azure静态网站配置多阶段部署 推荐超级课程: 本地离线DeepSeek AI方案部署实战教程【完全版】Docker快速入门到精通Kubernetes入门到大师通关课AWS云服务快速入门实战目录 【DevOps】使用Azure DevOps为Azure静态网站配置多阶段部署示例应用程序…

手写一个Tomcat

Tomcat 是一个广泛使用的开源 Java Servlet 容器,用于运行 Java Web 应用程序。虽然 Tomcat 本身功能强大且复杂,但通过手写一个简易版的 Tomcat,我们可以更好地理解其核心工作原理。本文将带你一步步实现一个简易版的 Tomcat,并深…

如何在Django中实现批量覆盖更新的示例

在使用Django进行开发时,数据的更新是一个常见的操作。有时候,我们需要对多个记录进行批量覆盖更新,这样可以提高效率,减少数据库的交互次数。本文将详细介绍如何在Django中实现批量覆盖更新,并提供示例代码来帮助你更…