C# 在文本文件中更改时需要toast/modal
每当有新的文本文件或现有文本文件中有一些更改时,我们需要在ASP.net页面上显示模式或Toast通知。以下是我们得出的结论:C# 在文本文件中更改时需要toast/modal,c#,asp.net,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)
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());
}
}
私有字符串htmsDir=“C:\\HTMS”;
私有字符串htmsDirPath=string.Empty;
文件系统监视程序;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
watcher=新文件系统观察程序(htmsDir);
watcher.Filter=“*.txt”;
watcher.Changed+=新文件系统EventHandler(watcher\u Changed);
watcher.EnableRaisingEvents=true;
}
}
私有无效观察程序\u已更改(对象发送方、文件系统目标)
{
尝试
{
String[]lines=System.IO.File.ReadAllLines(e.FullPath);
如果(线长度<1)
返回;
字符串行=行[lines.Length-1];
int计数器=1;
while(line==null | | line.Length<1)
{
计数器++;
如果(计数器>行长度)
打破
行=行[行.长度-计数器];
}
if(line==null | | line.Length<1)
返回;
如果(解析HTMSDATA(行))
{
UpdateForm();
}
}
捕获(异常){}
}
私有布尔解析HTMSDATA(字符串行){
返回true;
}
私有void UpdateForm()
{
尝试
{
回复。写下(“本”);
}
捕获(例外情况除外)
{
Common.logger.CreateDBLog(例如ToString());
}
}
但问题是像Response.Write()这样的函数代码>根本不工作!而且一开始也没有合适的例外
我们做错了什么?有没有更简单的方法