Easier FTP Management Class (.dll)





5.00/5 (3 votes)
.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!