Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I using C#.Net code to copy a file containing xml data to a location which is on a linux server. Soon after i copy the file i call a third party (java app) web-service that tries to consume the file copied. The third party service first tries to rename the file and in the process is getting an issue -Java File.renameTo method returns false. This suggests that the file is still being used/open by some other process.

The stream write code is given below

C#
using (StreamWriter file = new StreamWriter(filename)
                {
                    file.WriteLine(xml);
                    file.Close();
                }


// Code that calls the web service follows

The file that is copied may be huge in size (20-30 MB in some cases). Is there any way we can fully confirm that the file has been completely copied to the location and that no process is holding on to the file before i call the third party service. I am sure there not other process holding on other than the one copying the file.
Posted
Comments
Pikoh 11-Nov-14 3:04am    
To ensure the file is completely copied,why don't you copy it with another name (e.g. FileCoping.xml) and when the copy is finished,rename it to File.xml?
ajay_mathew 11-Nov-14 4:07am    
I could work with that. So, do u think, in the code after coming out of the closing bracket of using, the file could still be copying in the background a the file size is huge.
Richard MacCutchan 11-Nov-14 3:56am    
You are guessing why the File.renameTo method returns false, it could be one of many different reasons. Do some proper debugging to find out exactly what the error is before trying to fix a problem that may not exist.
Herman<T>.Instance 11-Nov-14 5:57am    
I have seen his behaviour when reading excel files with ODBC. That it took 5 minutes before the file could used by the program. Couldn't solve it either.
Bernhard Hiller 12-Nov-14 3:23am    
If you copy that file with e.g. Windows Explorer, do you face the same issue?

1 solution

VB
When you open the file in your application using FileStream or any similar class you have option of opening it in SharingMode or not. If you open it in SharingMode file will not be locked. If you open it in Non-Sharing mode, file will be locked.

Let me know if that is what you were looking for.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900