Click here to Skip to main content
15,038,237 members
Articles / Programming Languages / Basic
Tip/Trick
Posted 4 Oct 2015

Stats

10.2K views
246 downloads
6 bookmarked

Easier FTP Management Class (.dll)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
4 Oct 2015CPOL
.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:

VB.NET
UploadFile(FilePath, FTPAddress)

That command will really do this:

VB.NET
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:

VB.NET
Imports FTPFunctions.Functions

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

VB.NET
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:

VB.NET
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Member 12032084
Argentina Argentina
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --