[英]C# File Monitoring
This applies to a previous question I had asked.这适用于我之前提出的一个问题。 I have been trying to create an application that will allow me to monitor a file and alert me if the file has stopped changing.
我一直在尝试创建一个应用程序,它允许我监视文件并在文件停止更改时提醒我。 Someone recommended just checking the file size and using a timer in a Windows service.
有人建议只检查文件大小并在 Windows 服务中使用计时器。 I have taken their advice but I am not very good with Windows Services.
我接受了他们的建议,但我对 Windows 服务不是很好。 I am trying to do the file comparison between two files that I have created.
我正在尝试在我创建的两个文件之间进行文件比较。 Below is the code
下面是代码
public partial class Service1 : ServiceBase
{
Timer timer1 = new Timer();
string filePath = @"C:\Users\Test\Desktop\New folder\Test.txt";
string logPath = @"C:\Users\Test\Desktop\New folder\Log.txt";
string newLog = @"C:\Users\Test\Desktop\New folder\Log1.txt";
public Service1()
{
InitializeComponent();
}
public void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 60000;
timer1.Enabled = true;
timer1.AutoReset = true;
timer1.Start();
FileInfo f = new FileInfo(filePath);
long s1 = f.Length;
using (StreamWriter file = new StreamWriter(logPath))
{
file.WriteLine(s1.ToString());
}
}
protected override void OnStop()
{
timer1.Enabled = false;
}
public void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
FileInfo f = new FileInfo(filePath);
long s2 = f.Length;
using (StreamWriter file = new StreamWriter(newLog))
{
file.WriteLine(s2.ToString());
}
}
}
What I can not figure out is how to reference s1 in the timer_elapsed method.我想不通的是如何在 timer_elapsed 方法中引用 s1 。 I tried declaring it globally but it would not work.
我尝试在全球范围内声明它,但它不起作用。 I want to just be able to do something like this in the timer elapsed method.
我希望能够在计时器经过的方法中做这样的事情。
if (s1 == s2)
{
//send email
}
else
{
//don't do anything
}
Any advice would be much appreciated.任何建议将不胜感激。
You need the FileSystemWatcher class.您需要 FileSystemWatcher class。 There's settings for NotifyFilter / Path / IncludeSubdirectories.
NotifyFilter / Path / IncludeSubdirectories 有设置。
Also, this class raises a bunch of events for Changed, Created, Deleted, Renamed.此外,此 class 引发了一系列更改、创建、删除、重命名的事件。
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
just do it like this:这样做:
public partial class Service1 : ServiceBase
{
Timer timer1 = new Timer();
string filePath = @"C:\Users\Test\Desktop\New folder\Test.txt";
string logPath = @"C:\Users\Test\Desktop\New folder\Log.txt";
string newLog = @"C:\Users\Test\Desktop\New folder\Log1.txt";
long oldFileSize; // Add this line
public void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 60000;
timer1.Enabled = true;
timer1.AutoReset = true;
timer1.Start();
FileInfo f = new FileInfo(filePath);
oldFileSize = f.Length; // change this line
using (StreamWriter file = new StreamWriter(logPath))
{
file.WriteLine(s1.ToString());
}
}
public void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
FileInfo f = new FileInfo(filePath);
long s2 = f.Length;
using (StreamWriter file = new StreamWriter(newLog))
{
if (s2 == oldFileSize)
{
// Send Email
}
file.WriteLine(s2.ToString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.