Get file size - while inuse by another process
-
Hi I am trying to check if a file is being written by another process. I am using teh FileInfo (size) to see if the size (it is a log file) is changing. When another process is writing to the file my application is stuck.
string fPath = @"c:\temp\build.log"; FileInfo FI = new FileInfo(fPath); while (1>0) { if (lastvalue - FI.Length == 0) { iCounter++; lastvalue = FI.Length; if (iCounter > 20) { MessageBox.Show("times up"); break; } } else iCounter = 0; lastvalue = FI.Length; Thread.Sleep(SleepTime); }
Any idea could help.:confused::confused:Have a nice Day
-
Hi I am trying to check if a file is being written by another process. I am using teh FileInfo (size) to see if the size (it is a log file) is changing. When another process is writing to the file my application is stuck.
string fPath = @"c:\temp\build.log"; FileInfo FI = new FileInfo(fPath); while (1>0) { if (lastvalue - FI.Length == 0) { iCounter++; lastvalue = FI.Length; if (iCounter > 20) { MessageBox.Show("times up"); break; } } else iCounter = 0; lastvalue = FI.Length; Thread.Sleep(SleepTime); }
Any idea could help.:confused::confused:Have a nice Day
Hi! One idea that's working fine in some of my programs is trying to open the target file exclusively (with
FileShare.None
). If the file can be opened, no other process is accessing the file and thus the file has been created completely. If you get anIOException
then sleep for a few ms and try again. Just don't forget to close the file afterwards and include a timeout mechanism so your program doesn't lock up if the file isn't closed because of a bug in the other application.Regards, mav -- Black holes are the places where God divided by 0...
-
Hi! One idea that's working fine in some of my programs is trying to open the target file exclusively (with
FileShare.None
). If the file can be opened, no other process is accessing the file and thus the file has been created completely. If you get anIOException
then sleep for a few ms and try again. Just don't forget to close the file afterwards and include a timeout mechanism so your program doesn't lock up if the file isn't closed because of a bug in the other application.Regards, mav -- Black holes are the places where God divided by 0...
Thanks for the quick reply. I understand your suggestion but the problem is that the process holding the file is loading a message box and until i press "ok" the file is still open by it. The messagebox is not always present . I want to get a message when the file is not updated anymore .
Have a nice Day
-
Hi I am trying to check if a file is being written by another process. I am using teh FileInfo (size) to see if the size (it is a log file) is changing. When another process is writing to the file my application is stuck.
string fPath = @"c:\temp\build.log"; FileInfo FI = new FileInfo(fPath); while (1>0) { if (lastvalue - FI.Length == 0) { iCounter++; lastvalue = FI.Length; if (iCounter > 20) { MessageBox.Show("times up"); break; } } else iCounter = 0; lastvalue = FI.Length; Thread.Sleep(SleepTime); }
Any idea could help.:confused::confused:Have a nice Day
Hi use the FileSystemWatcher class
-
Thanks for the quick reply. I understand your suggestion but the problem is that the process holding the file is loading a message box and until i press "ok" the file is still open by it. The messagebox is not always present . I want to get a message when the file is not updated anymore .
Have a nice Day
I fear you're out of luck in this case. As long as the file is held open by the other application, this other application is free to modify the contents of the file at any given time. So you can never be sure that the other app will not write anything to the file. Depending on the architecture of the other app it could very well be that a background thread still writes to the file even though a message box is being displayed.
Regards, mav -- Black holes are the places where God divided by 0...
-
Hi use the FileSystemWatcher class