first off heres the code
When this code is run and a .pgn file is created the _watcher_Created event fires twice (thats expected)
i can get around that fairly easily...
my problem though is that i get different exceptions thrown when i run this method.
Sometimes i will get "FileNotFoundException"
and sometimes i will get "This file is in use by another program"
(Note a step through in the debugger works though; although it does call the event twice so my file is written twice.)
it seems as if with the FileNotFoundException that the watcher is doing its job so well that the file isnt actually created yet when the event fires.
The second seems to be that the file is in the process of being written to so therefore cannot be accessed.
My question is... How can i ensure that the file is ready to be read by my program. So that i can write its data to my file.
Is there a method that checks for a files status?
looks like im hitting the books on this one
i hope someone has some info for me
thanks
brandon
C#:
#region Declarations
FileSystemWatcher _watcher = new FileSystemWatcher("C:\\" ,"*.pgn");
#endregion
private void frmMain_Load(object sender, System.EventArgs e)
{
_watcher.IncludeSubdirectories = true;
_watcher.EnableRaisingEvents = true;
}
private void _watcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
StreamReader sr;
StreamWriter sw;
try
{
sr = new StreamReader(e.FullPath);
sw = new StreamWriter("AutoCompiledChessGames",true);
while (sr.Peek() != -1)
{
sw.WriteLine(sr.ReadLine());
}
sr.Close();
sw.Close();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
When this code is run and a .pgn file is created the _watcher_Created event fires twice (thats expected)
i can get around that fairly easily...
my problem though is that i get different exceptions thrown when i run this method.
Sometimes i will get "FileNotFoundException"
and sometimes i will get "This file is in use by another program"
(Note a step through in the debugger works though; although it does call the event twice so my file is written twice.)
it seems as if with the FileNotFoundException that the watcher is doing its job so well that the file isnt actually created yet when the event fires.
The second seems to be that the file is in the process of being written to so therefore cannot be accessed.
My question is... How can i ensure that the file is ready to be read by my program. So that i can write its data to my file.
Is there a method that checks for a files status?
looks like im hitting the books on this one
i hope someone has some info for me
thanks
brandon