c# txt文档的实时表现,用来检察发送汲取指令

[复制链接]
发表于 2025-10-16 03:36:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

通讯汗青按钮
  1.         private void uiButton1_Click(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 logf = new logF();
  6.                 logf.Show();
  7.             }
  8.             catch (Exception)
  9.             {
  10.                 throw;
  11.             }     
  12.         }
复制代码
主页面关闭函数(点击生存就为true true就不删除)
  1.         private void page1_FormClosed(object sender, FormClosedEventArgs e)
  2.         {
  3.             // 关闭应用时删除txt文件 为true就不删除
  4.             if (logF.savelog== false)
  5.             {
  6.                 if (File.Exists(logF.filePath))
  7.                 {
  8.                     File.Delete(logF.filePath);
  9.                 }
  10.             }
  11.             Application.Exit();
  12.         }
复制代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Sunny.UI;
  12. namespace WindowsFormsApp1
  13. {
  14.     public partial class logF : UIForm
  15.     {
  16.         public logF()
  17.         {
  18.             InitializeComponent();
  19.             Application.EnableVisualStyles();
  20.             //Application.Run(new MainForm());
  21.             MainForm();
  22.         }
  23.         private FileSystemWatcher fileWatcher;
  24.         public static string filePath = @"C:\log\run.txt";
  25.         public static bool savelog=false;
  26.         public void MainForm()
  27.         {
  28.             //// 初始化RichTextBox
  29.             //richTextBox1 = new RichTextBox();
  30.             //richTextBox1.Dock = DockStyle.Fill;
  31.             //this.Controls.Add(richTextBox1);
  32.             // 初始化FileSystemWatcher
  33.             fileWatcher = new FileSystemWatcher();
  34.             fileWatcher.Path = Path.GetDirectoryName(filePath);
  35.             fileWatcher.Filter = Path.GetFileName(filePath);
  36.             fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
  37.             // 绑定事件处理程序
  38.             fileWatcher.Changed += OnFileChanged;
  39.             fileWatcher.EnableRaisingEvents = true;
  40.             // 初始加载文件内容
  41.             LoadFileContent();
  42.         }
  43.         private void LoadFileContent()
  44.         {
  45.             try
  46.             {
  47.                 // 读取文件内容并显示在RichTextBox中
  48.                 using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  49.                 using (StreamReader reader = new StreamReader(fs))
  50.                 {
  51.                     richTextBox1.Text = reader.ReadToEnd();
  52.                 }
  53.             }
  54.             catch (Exception ex)
  55.             {
  56.                 MessageBox.Show("Error reading file: " + ex.Message);
  57.             }
  58.         }
  59.         private void OnFileChanged(object sender, FileSystemEventArgs e)
  60.         {
  61.             ////在UI线程上更新RichTextBox内容
  62.             //this.Invoke((MethodInvoker)delegate
  63.             //{
  64.             //    LoadFileContent();
  65.             //});
  66.             if (this.IsHandleCreated) // 确保句柄已存在
  67.             {
  68.                 this.BeginInvoke((MethodInvoker)delegate
  69.                 {
  70.                     LoadFileContent();
  71.                 });
  72.             }
  73.         }
  74.         private void logF_Load(object sender, EventArgs e)
  75.         {
  76.             //// 读取txt文件内容并显示在文本框中
  77.             //if (File.Exists(filePath))
  78.             //{
  79.             //    string content = File.ReadAllText(filePath);
  80.             //    richTextBox1.Text = content;
  81.             //}
  82.             //else
  83.             //{
  84.             //    MessageBox.Show("文件不存在!");
  85.             //}
  86.         }
  87.         private void uiButton7_Click(object sender, EventArgs e)
  88.         {
  89.             savelog = true;
  90.         }
  91.         private void uiButton1_Click(object sender, EventArgs e)
  92.         {
  93.             System.Diagnostics.Process.Start(@"C:\log");
  94.         }
  95.     }
  96. }
复制代码
log.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WindowsFormsApp1
  8. {
  9.     internal class log
  10.     {
  11.         public static void SaveLog(string log, string filename = @"C:\log\run.txt")
  12.         {
  13.             string pathA = filename.Substring(0, filename.LastIndexOf("\"));
  14.             if (!Directory.Exists(pathA))
  15.             {
  16.                 DirectoryInfo directoryInfo = new DirectoryInfo(pathA);
  17.                 directoryInfo.Create();
  18.             }
  19.             string filepathA = filename;
  20.             try
  21.             {
  22.                 String time = DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]");
  23.                 time += log;
  24.                 if (!File.Exists(filepathA))
  25.                 {
  26.                     using (StreamWriter sw = File.CreateText(filepathA))
  27.                     {
  28.                         sw.WriteLine(time);
  29.                         sw.Close();
  30.                     }
  31.                 }
  32.                 else
  33.                 {
  34.                     {
  35.                         using (StreamWriter sw = File.AppendText(filepathA))
  36.                         {
  37.                             sw.WriteLine(time);
  38.                             sw.Close();
  39.                         }
  40.                     }
  41.                 }
  42.             }
  43.             catch
  44.             {
  45.                 // MessageBox.Show("log异常" + ee.ToString());
  46.             }
  47.         }
  48.     }
  49. }
复制代码
  1. log.SaveLog("要保存的内容");
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表