Home › Forums › .NET libraries › Xceed SFTP/FTP for .NET › Using SendFile on a file that is open for updates
-
AuthorPosts
-
#21455 |
Hi,
I have a file that consistently gets updated several times a minutes (after a day, the file rolls to a new one). Every hour, I need to FTP that file so that I have a snapshot while the file is being updated. But, when I use SendFile, I get an FtpException – “The file item could not be opened for reading. Type: Xceed.FileSystem.DiskFile”.
Is there a way to work around this and get it to open the file with FileShare.ReadWrite permissions?
Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Sean (had 564 views)
Hi Sean,Unfortunately, this might not be under your control. It all depends on what FileShare value the application that updates the file uses when writing to it.When that application opens the file for writing, it has to use at least FileShare.Read for your own application to be able to open it for reading. If not, Windows will not allow your application to open the file. Unfortunately, often, applications use FileShare.None when opening files for writing.The FileShare parameter specifies what you allow other applications to do with a file you open. Not the other way around.I don’t know of a way to open a file for reading that has already been opened with FileShare.None by another application.Off the top of my head, my suggestion would be to write a loop that tries to copy the file to another location. That will be your snapshot. Then, upload that copy with FTP. The copy can be deleted after the upload has completed.Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Fawzi [Xceed] (had 153 views)
Thanks for the response.
We do have control of the file and it does have FileShare.ReadWrite (there’s other operations we perform that we need to view the file as it’s appended). Excel works OK, notepad works OK, I can send the file manually via FTP with FileZilla, but Xceed fails (version 5.3 BTW). The file may get updated frequently if that makes a difference (up to once a second) but it also fails if I just update every 30 seconds.
Here’s how the file is opened
Stream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Sean (had 211 views)
Hi Sean,
This is weird.
When you get the exception, can you “drill down” the inner exceptions and report them to us? This will allow us to know the real cause behind the exception you’re getting.
Each System.Exception object contains a InnerException property. You can loop on each inner exception, taking note of each exception along the way until InnerException is null.
Example (C#):
try
{
// TODO: Code that causes an exception
}
catch( Exception exception )
{
String information;
// Output some information about it
information = String.Format( “–>{0}: {1}\n{2}”, exception.GetType().Name, exception.Message, exception.StackTrace );
Console.WriteLine( information );
// Fetch the inner exception
exception = exception.InnerException;
// While there is an exception
while( exception != null )
{
// Output some information about it
information = String.Format( “–>Inner exception: {0}: {1}\n{2}”, exception.GetType().Name, exception.Message, exception.StackTrace );
Console.WriteLine( information );
// Fetch the inner exception
exception = exception.InnerException;
}
}Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Fawzi [Xceed] (had 634 views)
-
AuthorPosts
- You must be logged in to reply to this topic.