Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Easier FTP Management Class (.dll)

0.00/5 (No votes)
4 Oct 2015 1  
.dll for making the FTP management with VB.NET easier

Introduction

Have you ever had a problem while trying to figure out how to manage FTP Servers with VB.NET?

I have a solution. Well, it's not a solution, it's a class. :P

With this class, you'll be able to use simple commands like:

UploadFile(FilePath, FTPAddress)

That command will really do this:

Dim _FileInfo As New System.IO.FileInfo(_FileName)

        Dim _FtpWebRequest As FtpWebRequest = FtpWebRequest.Create(_UploadPath)

        _FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPassword)

        
        _FtpWebRequest.KeepAlive = False

        _FtpWebRequest.Timeout = 20000

        _FtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile

        _FtpWebRequest.UseBinary = True

        _FtpWebRequest.ContentLength = _FileInfo.Length

        Dim buffLength As Integer = 2048
        Dim buff(buffLength - 1) As Byte

        Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()

        Try
            Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()

            Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

            Do While contentLen <> 0
                _Stream.Write(buff, 0, contentLen)
                contentLen = _FileStream.Read(buff, 0, buffLength)
            Loop

            _Stream.Close()
            _Stream.Dispose()
            _FileStream.Close()
            _FileStream.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error when uploading file, try again.", _
		MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Easier, right?

Background

I wrote this class because I had some problems in the past managing an FTP server with VB.NET for a Messenger-like chat.

Problem!

I have a little problem with deleting a line with number from a file. If someone can help me with that, this .dll will be complete.

Using the Code

First of all, you have to add a reference to the .dll in your project, then, you have to import the class with:

Imports FTPFunctions.Functions

After using it, you have to set the User and password with:

FTPFunctions.Functions._FTPUser = "YourFTPServerUser"
FTPFunctions.Functions._FTPPassword = "YourFTPServerPassword"

You can write that whenever you want to set those variables.

You're ready to use it. You can use these functions:

UploadFile(FilePath, FTPAddress)
UploadTextToFTP(FTPAddress, TextToUpload)
DeleteFile(FTPAddress)
ReadLineFromFTP(FTPAddress, LineNumber)
'Add text to an existing file in FTP
AddTextToFTP(Address, TextToAdd)
'This one returns true or false
FileExists(Address)
'This too
DirectoryExists(Address)
DownloadFile(Address, PathWhereItWillBeSaved)
ReadTextFromFTP(Address)
MakeDirectory(Address)
DeleteDirectory(Address)
Rename(Address, NewName)
'This one isn't working yet
DeleteLineFromFTP(Address, LineNumber)

Hope it helps someone!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here