更改文本文件时需要烤面包/模态

时间:2019-06-01 11:56:37

标签: c# asp.net

每当有新的文本文件或现有文本文件发生更改时,我们都需要在ASP.net页面上显示“模态”或Toast通知。这是我们想出的:

private string htmsDir = "C:\\HTMS";
private string htmsDirPath = string.Empty;
FileSystemWatcher watcher;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        watcher = new FileSystemWatcher(htmsDir);
        watcher.Filter = "*.txt";
        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
        watcher.EnableRaisingEvents = true;
    }
}

private void watcher_Changed(object sender, FileSystemEventArgs e)
{
    try
    {
        String[] lines = System.IO.File.ReadAllLines(e.FullPath);
        if (lines.Length < 1)
            return;
        String line = lines[lines.Length - 1];
        int counter = 1;
        while (line == null || line.Length < 1)
        {
            counter++;
            if (counter > lines.Length)
                break;
            line = lines[lines.Length - counter];
        }
        if (line == null || line.Length < 1)
            return;
        if (ParseHTMSData(line))
        {
            UpdateForm();
        }
    }
    catch (Exception) { }
}

private bool ParseHTMSData(string line) {
    return true;
}

private void UpdateForm()
{
    try
    {
        Response.Write("this");
    }
    catch (Exception ex)
    {
        Common.logger.CreateDBLog(ex.ToString());
    }
}

但是问题是Response.Write();之类的函数根本无法工作!而且没有合适的异常开始。

我们在做什么错?有没有更简单的方法?

0 个答案:

没有答案