Home Forums .NET libraries Xceed SFTP/FTP for .NET Code example for catching exceptions when connecting, receiving files, sending files

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

    I am using a trial version to determine whether it would work for us. My OS is Windows CE and I am using ftp component for Compact Framework.

     

    What i am looking for is a tested, comprehensive way to log in, receive files, send files while trapping all the exceptions properly and informing the user.

    For example, at login, my application does 3 things:

    1) logs the user in to FTP server

    2) receives 1st file

    3) receives 2nd file

     

    While these actions are being performed, several things can go wrong, such as user’s credentials are wrong, file 1 not found, file 2 not found, server hangs, etc etc. 

     

    How do I catch all these and inform the user accordingly? I see that there are several Exception classes,  but there is no clear explanation what each does. FtpreplyException is raised when the credentials are wrong as well as when the file is not found. The only way for me to differentiate between these two situations is to rely on the FTPReply message ID, which i would rather not do. Is that the only way?

     

    Can someone provide a well tested piece of code for this? Error catching is the only reason why we are switching to a commercial component, so I would like to do this right.

     

    Thanks!

     

     

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

    Xceed Support
    Member
    Post count: 5658

    Hi Jenny,

    Your issue was resolved in a support case; however i will post the answer as a reference to other Xceed clients.

    Answer:

    We do have an exception called: “ItemDoesNotExistException”.
    This exception will be thrown when performing an operation on a FileSystemItem object that does not yet exist.

    But you will have to catch it by code:

    Code Sample:
    ————
    try
    {
      ZippedFile f = new ZippedFile( new DiskFile( @”c:\test.zip” ), @”\file.txt” );
      f.Delete();
    }
    catch( ItemDoesNotExistException err )
    {
      Console.WriteLine( err.Message );
    }

    For a list of all exception types for our FileSystem core, refer this link in our online documentation:
    http://doc.xceedsoft.com/products/XceedFileSystem/Exceptions.html

    Also, for a non trivial code sample for Ftp for .NET CF, you can check out the code of the sample application that was downloaded with the component; It is usually located in this file:
    “C:\Xceed Component Samples\Xceed FTP for .NET Compact Framework 4.1\Samples\CSharp\ClientFtp”

    Best regards,

    Alain Jreij
    Technical Support

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Alain [Xceed] (had 486 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Alain,

     

    the issue actually hasn’t been resolved – i am back on this project now and i emailed your support team this morning, have received no reply yet. Last time i had to wait a month for your reply. Let’s hope this doesn’t repeat itself. 

     

    Anyways, what i am looking for is not an exception that is thrown when I look for a file on my local drive. What I am looking for is a SURE WAY TO TELL WHETHER A FILE I AM TRYING TO FTP OVER DOESN’T EXIST IN THE REMOTE LOCATION.

     Sorry for the caps, but I hope i got my point across this time.my question had nothing to do with FileSystem object.

     

    so if I do something like:

     

    try{

    ftpClient.receivefile(myfilename);

    } catch (FileDoesNOTExistException ex) {

    MessageBox.Show(“file you are trying  to retrieve doesn’t exist”);

    }

     

    unfortunately the exception you provided me with is not being thrown when the file is not found at the remote location. I thought I was being pretty clear in my question. Please read my question again. If you could reply within a day or 2 that would be great, i think waiting for a month was a bit of an overkill.

     

     

    Much regards

     

     

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

    Xceed Support
    Member
    Post count: 5658

    Hi Jenny,

    I apologize if my last reply was not clear enough, i will try to make this one more detailed.

    All our DATA MANIPULATION COMPONENTS share the same core which is the Xceed FileSystem (check attached image), for example, in order to list the files contained in a remote ftp server you should do something like this:

    CODE SAMPLE:
    ————
    using( FtpConnection connection = new FtpConnection( “ftp.server.com” ) )
    {
      AbstractFolder folder = new FtpFolder( connection );

      foreach( FileSystemItem item in folder.GetItems( true ) )
      {
        System.Diagnostics.Debug.WriteLine( item.FullName );
      }
    }

    (this and other samples can be found in our online documentation here:
    http://doc.xceedsoft.com/products/XceedFileSystem/ -> Basic Concepts -> FTP capabilities)

    So now, back to your question, which was how to differentiate between:
    1) file not found at the specified location:
    ——————————————–
    -Create an Abstract file
    -Use the function Getfile of the rootFolder and give it the full path of that file as a parameter.
    -Call the Property .Exists of that file.

    2) ftp server is down / network issues:
    —————————————
    -Create an Ftpfolder and assign it a connection object
    -Call the folder’s .Exist property which will force the connection to be established.
    -Put this inside a try/catch block that catches an FtpException.

    In order to try this, please open our FileSystem Snippet Explorer:
    Start -> All Programs -> Xceed Components -> Our components in action! ->
    Xceed FileSystem Snippet Explorer -> 4.1.1. Main Classes

    Change the content of the Example() method to this:
    ===================================================
    using( FtpConnection connection = new FtpConnection( “localhost”, 21, “user”, “pass” ) )
          {
            // The same code that lists the contents of a local folder on your drive…

            connection.SynchronizingObject= m_resultsForm;
           
            connection.TraceWriter = Console.Out;

            try
            {
              AbstractFolder rootFolder = new FtpFolder( connection );
           
              // This will force the connection to the Ftp server. If
              // it fails, this means the host is not reachable.
              bool rootExists = rootFolder.Exists;
            }
            catch(Exception )
            {
              Console.WriteLine( “Unable to connect to server”);
              return;
            }
           
            // change the path to some file inside your localhost folder
            AbstractFile file = rootFolder.GetFile(@”KT_Vault\KT_UserConnected\KT_cu.zip”);

            bool exists = (file != null) ? file.Exists : false;

            Console.WriteLine( “Exists ” + exists + ” with fullname ” + file.FullName);
            Console.Read();     
           
          }

    Best regards,

    Alain Jreij,

    Xceed

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Alain [Xceed] (had 420 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Alain

     

    Thanks for you reply. I tried it and it works. Before we close this issue, I would like  to confirm something with you. I forgot  to mention that I would also like to differentiate between a situation when the user enter wrong credentials. So to summarize, i need a sure, robust way (not relying on FTP reply code) to tell btwn these:

    1) server/network down

    2) wrong credentials

    3) file/(s) not found at the specified location.

     

    Looking at the code you provided in your last reply, i saw that FileSystemException was being thrown in the code below, whether user entered wrong credentials or server down.

     try
                    {
                        rootFolder = new FtpFolder(connection);

                        // This will force the connection to the Ftp server. If
                        // it fails, this means the host is not reachable.
                        bool rootExists = rootFolder.Exists;
                    }
                    catch ( Exception ex)
                    {
                        Console.WriteLine(“Unable to connect to server ” + ex.GetType().ToString() );
                        return;
                    }

     

    To solve this, i used TestConnection() function. After initial testing it turned out that I can indeed use this function as it throws different exceptions depending on the case:

    1) server/network down – FtpIOException or FtpInternalException

    2) wrong credentials – FtpReplyException

     

    i can deal with case 3) using the code you provided.

     

    If all of this works the way I think it does, then I am covered. Please confirm.

     

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

    Xceed Support
    Member
    Post count: 5658

    Hi Jenny,

    You are right.

    You can use TestConnection() to differentiate between 1) and 2), and what i suggested in 3).

    Regards,

    Alain Jreij,

    Xceed Technical Support

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Alain [Xceed] (had 406 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Alain

     

    one more question regarding TestConnection().

    I have noticed that the first time I use it, it takes about 7 second while subsequent calls only take 1 second. Then after awhile (1 hour) it’s 7 seconds again. It is almost as if some unmanaged objects have been created and cached. 

    Is that the case? Is there a way to give users consistent login time? 7 seconds vs 1 is a big difference. I know I am going to be asked why this is the case.

     

    Can you shed some light on this please?

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

    User (Old forums)
    Member
    Post count: 23064

    Hi Jenny,

    Using a timer in the code before and after a call to all methods that could be causing the issue (and then repeating the same process within this method until it is possible to identify the reason why this is happing) would be the way to go.  It’s usually not too complicated to test the duty cycle of a method/procedure if there is no way that the duty cycle can vary.  However, considering everything that gets involved in an ftp session (client side, server side), network traffic, databases access, finding the origin of the 6 seconds difference could be quite a journey.

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

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