I wondering if there is a way to monitor a .txt file without locking it and have the newly appended lines parsed in order to be sent to a database.
I know FileSystemWatcher will monitor the txt file without locking it and notify me if a change has occurred. The problem is that it does not tell the lines of data that has been appended. All of the examples I have looked at exclude the part of displaying newly appended lines or doing any processing of the new data. My program has the following structure:
static void Main(string[] args)
{
//string line;
//createWatcher (FileSystemWatcher)
foreach (string line in getLines(@"C://Documents/log.txt"))
{
string teststring = line;
string[] parts = line.Split(' ', ',', '-', '>', '[', ']');
StringBuilder builder = new StringBuilder();
foreach (string h in parts)
{
builder.Append(h).Append(" ");
}
string result = builder.ToString();
string cleanedString = System.Text.RegularExpressions.Regex.Replace(result, @"\s+", " ");
string trimString = cleanedString.Trim();
trimString = trimString.Remove(trimString.Length - 7);
//Console.WriteLine(trimString);
//Process new lines (parse/split)
//Connect and send to database
}
}
So my objective is to monitor a .txt file without locking it.
Does anyone know how to best approach this? or have a good tutorial?
Your best bet is for the code fired off of the file system watcher to keep track of the Position in the stream that's reading the file. Here's some pseudocode:
if(no stored position)
start at beginning of file
else
{
streamOfFile.Seek(storedPosition)
read until end of file
store stream.Postition
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With