Home Forums .NET libraries Xceed SFTP/FTP for .NET AbstractFile.CopyTo of open file

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21043 |

    I need to copy a bunch of files, and some of them could currently be open with the app that created them.

    I have an array of AbstractFiles that I loop through and on each file, I use the method:

    file.CopyTo(fileSystemEvents,

    null, new DiskFile(tempFileName), true);

    On any files that are currently open, I receive an exception. 

    I had a previous implementation working using unmanaged code through a Win32 interface that worked fine, but I wanted to move over to use all managed code and the Xceed libraries.

    Is there an approach I can use that won’t throw an exception if the file happens to be open?

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Chris (had 4120 views)

    User (Old forums)
    Member
    Post count: 23064

    On which OS are you?  Or is the file open by a special application?  Normally, when simply copying files, the OS should not lock the file.

    In any case, you can use the ItemException event handler, and ask the user to close the file, so it can be copied.

    e.g.:

    private static void run()

    {

        FileSystemEvents events = new FileSystemEvents();

        events.ItemException +=

    new ItemExceptionEventHandler(events_ItemException);

        AbstractFile file = new DiskFile( SomeFile );

        AbstractFolder destination = new DiskFolderDestinationFolder );

        file.CopyTo( events,

    null, destination, true );

    }

    static void events_ItemException( object sender, ItemExceptionEventArgs e )

    {

        //ask the user the close the file

        //if he does, try again

            e.Action =

    ItemExceptionAction.Retry;

        //if he does not, ignore the file

            e.Action =

    ItemExceptionAction.Ignore;

    }

     

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by André (had 486 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for the reply.

    Our application is a server application, so it will be running on Windows 2003 server or Windows 2008 server.  The application that’s holding the files open is Exchange 2003/20007.  Exchange writes a message tracking log (basically a text log of activity) and it always holds the current day’s log open.

    You can copy the current day’s log file (the open one) with a regular dos command.  And like I said, with our Win32 copy (which we use because it gives us progress events) we can copy it as well.  But when we use AbstractFile.Copyto it generates an error.

    We do something like this:

    AbstractFile[] files = folder.GetFiles(false, new object[] { “*.log” });
                foreach (AbstractFile file in files)
                {

    file.CopyTo(

    new DiskFile(“c:\\destination\filename”), false);

    }

    We’re a source code member, so maybe I’ll check the source on this, but it’s just strange and I was wondering if you had seen this before.

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Chris (had 632 views)

    User (Old forums)
    Member
    Post count: 23064

    CopyTo() eventually calls the .NET method
     
    System.IO.FileInfo.Open( FileMode.Open, FileAccess.Read, FileShare.Read );
     
    This means that if the file is already opened by another process, we only require the file for reading. So it depends on whether the other application, Exchange in your case, has specified a sharing mode that includes reading when it opened the files.
     
    What exact exception message and stack trace do you get when you call CopyTo()?

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by André (had 3868 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.