Home Forums .NET libraries Xceed SFTP/FTP for .NET Simple FTP in a Console app

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

    I’m trying to build a very simple function that will programmatically FTP a single file to a server. I’d also like to capture the results of that FTP to make sure it all got sent properly. Unfortunately, the only example I can find on the site is far more complicated than I need. I don’t want a full-blown app, just a simple function. How would I go about doing this?

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

    User (Old forums)
    Member
    Post count: 23064

    Here is a simple SendFile implementation, with “correct” error handling.

    <code> private static bool SendFile(
    string serverAddress,
    string userName,
    string password,
    string sourceFilename,
    string destFolder,
    TextWriter writer )
    {
    FtpClient client = new FtpClient();
    client.TraceWriter = writer;

    try
    {
    client.Connect( serverAddress );

    try
    {
    client.Login( userName, password );

    if( ( destFolder != null ) && ( destFolder.Length > 0 ) )
    {
    client.ChangeCurrentFolder( destFolder );
    }

    client.SendFile( sourceFilename );
    }
    finally
    {
    client.Disconnect();
    }

    return true;
    }
    catch( Exception except )
    {
    if( writer != null )
    {
    writer.WriteLine( “Error sending file {0}:\n{1}”,
    sourceFilename,
    except.Message );
    }

    return false;
    }
    finally
    {
    client.TraceWriter = writer;
    }
    }
    </code>

    In a console application, you could call the above like this:

    <code> SendFile(
    “ftp.yourserver.com”, “foo”, “bar”,
    @”d:\appnote.txt”,
    “pub”,
    Console.Out );
    </code>

    Hope this helps.

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

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