花哨的 2022-03-22 09:44 采纳率: 65.4%
浏览 216
已结题

c#中假设我有一个txt文档,怎么做到一个程序写入的同时另一个程序可以读取

c#中假设我有一个txt文档,怎么做到一个程序写入的同时另一个程序可以读取

  • 写回答

3条回答 默认 最新

  • 全栈小5 全栈领域优质创作者 2022-03-22 11:39
    关注

    温馨提示:有其他疑问可加咨询
    1、效果如下
    1)在后台执行一个方法,每秒都往txt文件写入信息
    2)同时在页面定时读取内容

    img

    2、代码如下
    1)后端代码

    using Microsoft.AspNetCore.Mvc;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test.core._31.Controllers
    {
        public class ReadWriteTxtController : Controller
        {
            public static string myLocalPath = @"E:\test.txt";
    
            public static void WriteFile()
            {
                try
                {
                    var FilePath = myLocalPath;
                    var now = DateTime.Now;
                    string Content = now.ToString("yyyy年MM月dd日 HH:mm:ss\r\n");
    
                    System.IO.File.AppendAllText(FilePath, Content);
    
                }
                catch (Exception exception)
                {
    
                }
                finally
                {
    
                }
            }
    
            static string ReadFile()
            {
                string content = "";
    
                try
                {
                    using (StreamReader sr = new StreamReader(myLocalPath, Encoding.Default))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            content = line.ToString() + "\r\n" + content;
                        }
                    }
                }
                catch (Exception exception)
                {
    
                }
                finally
                {
    
                }
    
                return content;
            }
    
    
            public IActionResult Index()
            {
                Task.Run(() => {
    
                    while (true)
                    {
                        System.Threading.Thread.Sleep(1000);
                        WriteFile();
                    }
                });
    
                return View();
            }
    
    
            public IActionResult Read()
            {
                string test1 = ReadFile();
                test1 = test1.Replace("\r\n", "<br/>");
    
                return Json(test1);
            }
        }
    }
    

    2)前端代码

    @{
        Layout = null;
    }
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no" />
    
    <div id="info">
    
    </div>
    
    <script src="https://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
    <script type="text/javascript">
        $(function () {
    
            setInterval(function () {
    
                $.ajax({
                    url: '/ReadWriteTxt/Read',
                    data: {},
                    dataType: 'json',
                    type: 'test',
                    success: function (data) {
                        
                        $("#info").html(data);
                    }
                });
            }, 500);
            
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 已采纳回答 3月22日
  • 创建了问题 3月22日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果