Wait Until File Is Completely Written
There is only workaround for the issue you are facing.
Check whether file id in process before starting the process of copy. You can call the following function until you get the False value.
1st Method, copied directly from this answer:
private bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
2nd Method:
const int ERROR_SHARING_VIOLATION = 32;
const int ERROR_LOCK_VIOLATION = 33;
private bool IsFileLocked(string file)
{
//check that problem is not in destination file
if (File.Exists(file) == true)
{
FileStream stream = null;
try
{
stream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (Exception ex2)
{
//_log.WriteLog(ex2, "Error in checking whether file is locked " + file);
int errorCode = Marshal.GetHRForException(ex2) & ((1 << 16) - 1);
if ((ex2 is IOException) && (errorCode == ERROR_SHARING_VIOLATION || errorCode == ERROR_LOCK_VIOLATION))
{
return true;
}
}
finally
{
if (stream != null)
stream.Close();
}
}
return false;
}
Wait for file to be freed by process
A function like this will do it:
public static bool IsFileReady(string filename)
{
// If the file can be opened for exclusive access it means that the file
// is no longer locked by another process.
try
{
using (FileStream inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
return inputStream.Length > 0;
}
catch (Exception)
{
return false;
}
}
Stick it in a while
loop and you have something which will block until the file is accessible:
public static void WaitForFile(string filename)
{
//This will lock the execution until the file is ready
//TODO: Add some logic to make it async and cancelable
while (!IsFileReady(filename)) { }
}
Checking if file is completely written
Does the producer process close the file when its finished writing? If so, trying to open the file in the consumer process with an exclusive lock will fail if the producer process is still producing.
How to test if a file is complete (completely written) with Java
You could use an external marker file. The writing process could create a file XYZ.lock before it starts creating file XYZ, and delete XYZ.lock after XYZ is completed. The reader would then easily know that it can consider a file complete only if the corresponding .lock file is not present.
bash script inotifywait wait for file completely written before proceding
As pointed out by pacholik close_write as event option alone is sufficient:
inotifywait -m -e close_write $1 | while read line
Python wait and check if file is created completely by external program
This method apparently only works on Windows (ref. comment below), and relies on the fact that your external program only open and closes the file once during the creation of it.
import time
filename = 'my_file.txt'
while True:
try:
with open(filename, 'rb') as _:
break
except IOError:
time.sleep(3)
If you want to set a maximum limit to the number of access attempts, you can do something like this:
import time
filename = 'my_file.txt'
max_i = 10
for i in xrange(max_i):
try:
with open(filename, 'rb') as _:
break
except IOError:
time.sleep(3)
else:
raise IOError('Could not access {} after {} attempts'.format(filename, str(max_i)))
Related Topics
Writing to a Textbox from Another Thread
Reading File Input from a Multipart/Form-Data Post
Change System Date Programmatically
Richtextbox (Wpf) Does Not Have String Property "Text"
Why Does .Net Use Int Instead of Uint in Certain Classes
When Should "Sqldbtype" and "Size" Be Used When Adding SQLcommand Parameters
"A Namespace Cannot Directly Contain Members Such as Fields or Methods"
Convert String to Datetime in C#
C# Class to Parse Webrequestmethods.Ftp.Listdirectorydetails Ftp Response
Decompressing Gzip Stream from Httpclient Response
How to Set Extended File Properties
Is a Memory Leak Created If a Memorystream in .Net Is Not Closed
How to Get All Constants of a Type by Reflection
Why Do C# Multidimensional Arrays Not Implement Ienumerable<T>