I am continuously polling a file to see if it is updated i.e. if some cookie is set in the file by the browser. If I am not running the application, the browser sets the value of cookie into it but if I am running the application, my file cannot be written to. Is there some way to read the file so that it can also be written to simultaneously? Following is the code I’m using:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create a timer that polls once every 5 seconds
var timer = new System.Threading.Timer(TimerProc, null, 5000, 5000);
Console.WriteLine("Polling every 5 seconds.");
Console.WriteLine("Press Enter when done:");
Console.ReadLine();
timer.Dispose();
}
static int TickCount = 0;
static void TimerProc(object state)
{
++TickCount;
Boolean found = false;
Console.WriteLine("tick {0}", TickCount);
string curFile = @"C:\Users\ACP\AppData\Local\ETH Zuerich\xul_seb\Profiles\cookies.sqlite-wal";
string line;
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
//Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
StreamReader file = new StreamReader(curFile);
while ((line = file.ReadLine()) != null)
{
if (line.Contains("SEB"))
{
found=true;
}
}
if (found)
{
Console.WriteLine("String exists in the file.");
}
else
{
Console.WriteLine("String does not exist in the file.");
}
file.Close();
}
}
}
Create a
FileStreamin read mode without locking it: