sending file telegram bot VB.NET - vb.net

please help, I want to send a file (android.apk) from the bot telegram to the user, but the file the user received is not in the format (apk), how do I resolve this. I use vb.net and the Telegram.Bot library. this my code:
Public Async Function sendMessageFile(ByVal destID As String, ByVal Path As String, Optional ByVal
DescriptionFile As String = "") As Task
Try
Dim objReader As StreamReader
Dim objFS As FileStream
Dim errorf As String = ""
Try
If File.Exists(Path) Then
objFS = New FileStream(Path, FileMode.Open)
objReader = New StreamReader(objFS)
Await bott.SendDocumentAsync(destID, objFS, DescriptionFile)
objReader.Close()
objFS.Close()
objReader.Dispose()
objFS.Dispose()
objFS = Nothing
objReader = Nothing
Else
errorf = "File not found!"
End If
Catch ex As Exception
errorf = ex.Message
End Try
Catch e As Exception
log("sendMessageFile - " & e.Message)
End Try
End Function
'// this code sending file to user
Case "/download"
Await bott.SendChatActionAsync(ID, Telegram.Bot.Types.Enums.ChatAction.UploadDocument)
Const file As String = "C:\Resources\debug.apk"
Await sendMessageFile(ID, file, "TESTING")
'// screenshoot
https://prnt.sc/qf09sn

Related

Google Drive API v3 VB.NET Upload To Spesific Folder

Please Help, i'm trying to upload file to specific folder in my google drive using Vb.net. but, i'm googling for hours and not get working code. i cant sleep because this. here is my code:
Private Sub UploadFile(FilePath As String)
Try
If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService()
Dim TheFile As New File()
TheFile.Name = "Database Sekretariat.accdb"
TheFile.Description = "A test document"
'TheFile.MimeType = "text/plain"
TheFile.Parents(0) = "1uMeTMRtvhm5_98udPmV8kp19aGtrmeQj"
Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
Dim Stream As New System.IO.MemoryStream(ByteArray)
Dim UploadRequest As FilesResource.CreateMediaUpload = Service.Files.Create(TheFile, Stream, TheFile.MimeType)
UploadRequest.Upload()
Dim file As File = UploadRequest.ResponseBody
MsgBox("Upload Selesai " & file.Name & "")
Catch ex As Exception
MsgBox("Upload Gagal")
End Try
End Sub
use it in the following way, it is functional
First, i look for the ID folder
Public Sub SearchFolder()
IDFolderSave = String.Empty 'Global Variable
Try
Dim findrequest As FilesResource.ListRequest = Service.Files.List
Dim listFolder As Data.FileList = findrequest.Execute
For Each item As File In listFolder.Files
If item.MimeType = "application/vnd.google-apps.folder" Then
If item.Name = "NameFolder" Then
IDFolderSave = item.Id.ToString
Exit For
End If
End If
Next
Catch ex As Exception
Throw ex
End Try
'MsgBox("Folder id: " + idFolder)
End Sub
Second, upload file
Public Sub UploadFileInFolder()
Dim FilePath As String = String.Empty
FilePath = "C:\Icons\Loadding.gif"
Dim plist As List(Of String) = New List(Of String)
SearchFolder()
plist.Add(IDFolderSave) 'Set parent folder
If (System.IO.File.Exists(FilePath)) Then
Dim fileMetadata = New File() With {
.Name = "Test",
.Parents = plist
}
Dim request As FilesResource.CreateMediaUpload
Using stream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open)
request = Service.Files.Create(fileMetadata, stream, "application/octet-stream")
request.Fields = "id, parents"
request.Upload()
End Using
Dim file As File = request.ResponseBody
IDFileShared = file.Id
SharedFile()
MsgBox("File upload: " + file.Id)
Else
MsgBox("File does not exist: " + FilePath)
End If
End Sub
I hope it helps you

FTP File Not found code 550

I am trying to fix a program that another developer has developed. What I'm trying to fix is everytime the program attempts to upload a file it would return a 'File . not found (code=550)'. The weird thing is that the actual file for some reason would actually upload... When I received a copy of the code to fix I tried the program out myself and it gives me the same error yet the file actually doesn't upload.
The code Imports Utilities.FTP (Which I haven't been able to find a lot of documentation on online..) and the exception is thrown after the ftp.CopyToFTP() is run. CopyToFTP basically copies the file to the FTP site directory you place.
Does anyone have any way to trouble shoot this? or a solution?
Private Sub FtpOutputFile(ByVal atts As List(Of String))
Dim inStream As StreamReader
Dim parseLn() As String
Dim ftp As New Utilities.FTP.FTP()
Try
inStream = New StreamReader(File.OpenRead(Me.Directory & "ftp.dat"))
'Setup FTP object
While inStream.EndOfStream = False
parseLn = Split(inStream.ReadLine(), ",")
Select Case parseLn(0)
Case "Destination"
ftp.Destination = parseLn(1)
Case "Expiration"
ftp.Expiration = parseLn(1)
Case "Host"
ftp.FTP_Host = parseLn(1)
Case "Password"
ftp.FTP_Password = parseLn(1)
Case "Timeout"
ftp.FTP_Timeout = parseLn(1)
Case "UserName"
ftp.FTP_UserName = parseLn(1)
End Select
End While
inStream.Close()
Dim MyEnum As IEnumerator(Of String) = atts.GetEnumerator
While MyEnum.MoveNext 'I added this? Before it was just the directory but this would be the entire file?
ftp.Source = Path.GetDirectoryName(MyEnum.Current) + "\DailyExcel" & DailyDate.ToString("yyyyMMdd") & ".csv"
If Not ftp.CopyToFTP() Then
Throw New Exception(ftp.ErrorMessage)
End If
End While
Catch ex As Exception
Trace.WriteLine(Date.Now.ToString("MM/dd/yyyy HH:mm") & ": " & ex.Message & vbNewLine & ex.StackTrace)
MsgBox(ex.Message())
End Try
End Sub
my .dat file is as follows:
Destination,excelfiles/Test/
Expiration,30
Host,myftp.mydomainname.com
Password,password
Timeout,5000
UserName,username
FTP error code 550 is access denied. Are you sure the server is configured properly?
Thanks to Sam Makin I was able to figure out the error. It seemed the file would download all data from an FTP directory and delete any files with a last modified date of over 30 days ago. If it wasn't it added it to a array to be compared to which was cross referenced with a local directory to determine what files should be uploaded.
The problem is when it downloaded all files it included the files '.' and '..' Which from what I know I'm going to assume that means current directory and parent directory. So it would see that the directory was created awhile ago attempt to delete it and it would say that the file wasn't found.
I don't know about all of hat nonsense you coded but a few rules,
You should NOT generate a file during the FTP (or any instantiated connection type) you want to keep the live time down to a absolute minimum for security and efficiency reasons.
Essentially, other than the procedural style the coder adapted to there isn't anything else that i can see that is politically wrong with the code.
550 is specifically a Access denied error. And that is not the entire error message, it should also tell you if it is a connection to the server that is being denied or the users credentials.
Your team doesn't seem to familiar with the FTP procedures in VB.net, take a look at my FTP i wrote for a friend.
Public Class FTP
'-------------------------[BroCode]--------------------------
'----------------------------FTP-----------------------------
Private _credentials As System.Net.NetworkCredential
Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
setCredentials(_FTPUser, _FTPPass)
End Sub
Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
_FtpWebRequest.Credentials = _credentials
_FtpWebRequest.KeepAlive = False
_FtpWebRequest.Timeout = 20000
_FtpWebRequest.Method = System.Net.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, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
responseStream.CopyTo(fs)
responseStream.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
Dim ret As New List(Of String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
Dim FileData As String = _reader.ReadToEnd
Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
For Each l As String In Lines
ret.Add(l)
Next
_reader.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return ret
End Function
Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
_credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
End Sub
End Class

VB.NET FTP Downloaded file corrupt

I want to download all file that located on FTP site. I break into 2 operations. First is to get the list of all files in the directory. Then I proceed to second which download each file. But unfortunately, the downloaded file is corrupt. Worst over, it affect the file on FTP folder. Both folder contains corrupt file. From a viewing thumbnails image to default image thumbnails. How this happen and to overcome this? Below are my code; full code class. I provide as reference and guide to do a correct way :)
Private strTargetPath As String
Private strDestPath As String
Private Sub frmLoader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
objSQL = New clsSQL
If objSQL.SQLGetAllData Then 'Get data from SQL and insert into an arraylist
strTargetPath = "ftp://192.168.1.120/image/"
strDestPath = "D:\Application\FTPImg\"
ListFileFromFTP()
End If
Catch ex As Exception
strErrMsg = "Oops! Something is wrong with loading login form."
MessageBox.Show(strErrMsg & vbCrLf & "ExMsg: " & ex.Message, "Error message!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub ListFileFromFTP()
Dim arrFile As ArrayList
Dim strPath As String
Try
Dim reqFTP As FtpWebRequest
reqFTP = FtpWebRequest.Create(New Uri(strTargetPath))
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential("user", "user123")
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory
reqFTP.Proxy = Nothing
reqFTP.KeepAlive = False
reqFTP.UsePassive = False
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
Dim sr As StreamReader
sr = New StreamReader(reqFTP.GetResponse().GetResponseStream())
Dim FileListing As String = sr.ReadLine
arrFile = New ArrayList
While FileListing <> Nothing
strPath = strTargetPath & FileListing
arrFile.Add(strPath)
FileListing = sr.ReadLine
End While
sr.Close()
sr = Nothing
reqFTP = Nothing
response.Close()
For i = 0 To (arrFile.Count - 1)
DownloadImage(arrFile.Item(i))
Next
Catch ex As Exception
strErrMsg = "Oops! Something is wrong with ListFileFromFTP."
MessageBox.Show(strErrMsg & vbCrLf & "ExMsg: " & ex.Message, "Error message!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function DownloadImage(ByVal strName As String) As String
Dim ftp_request As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Dim strOldName As String
Dim strNewName As String
Dim withoutextension As String
Dim extension As String
Try
strOldName = strTargetPath & strName
withoutextension = Path.GetFileNameWithoutExtension(strOldName)
extension = Path.GetExtension(strOldName)
strNewName = strDestPath & withoutextension & extension
ftp_request = CType(System.Net.FtpWebRequest.Create(strName), System.Net.FtpWebRequest)
ftp_request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
ftp_request.Credentials = New System.Net.NetworkCredential("user", "user123")
ftp_request.UseBinary = True
ftp_request.KeepAlive = False
ftp_request.Proxy = Nothing
Dim response As FtpWebResponse = DirectCast(ftp_request.GetResponse(), FtpWebResponse)
Dim responseStream As IO.Stream = response.GetResponseStream
Dim fs As New IO.FileStream(strNewName, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
responseStream.Close()
response.Close()
Catch ex As WebException
Dim response As FtpWebResponse = ex.Response
If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
MsgBox(response.StatusDescription)
Return String.Empty
Else
MsgBox(response.StatusDescription)
End If
End Try
End Function
For download file, this is the right code that succeed:
Private Sub Download(ByVal strFTPPath As String)
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
Dim outputStream As New FileStream(strDestPath & strImgName, FileMode.Create)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri(strFTPPath)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential("user", "user123")
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
ftpStream.Close()
outputStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
Throw New Exception(ex.Message.ToString())
End Try
End Sub
There is something strange in your code. You are closing the responseStream twice.
And you are using ftp request method uploadFile. But then downloading it.
Instead of using the ftp stuff use this instead. Its much more simple and your outsourcing the buffers and streams to microsoft, which should be less buggy than us.
Dim webClient = new WebClient()
webClient.Credentials = new NetworkCredential("user", "user123")
dim response = webClient.UploadFile("ftp://MyFtpAddress","c:\myfile.jpg")
dim sResponse = System.Text.Encoding.Default.GetString(response)

How to create directory on FTP server

I am using the following code for creating the folder on FTP server ; But its not working in my case :-
Dim sFilePath as string =filepath
Dim ftpResponse1 As FtpWebResponse
Dim ftpRequest1 As FtpWebRequest
Dim IsExists1 As Boolean = True
ftpRequest1 = CType(FtpWebRequest.Create(sFilePath), FtpWebRequest)
ftpRequest1.UseBinary = True
ftpRequest1.Credentials = New NetworkCredential(ZXFTPUSER, ZXFTPPASS)
ftpRequest1.UsePassive = True
ftpRequest1.Method = WebRequestMethods.Ftp.MakeDirectory
'ftpRequest1.KeepAlive = False
'ftpResponse1 = ftpRequest1.GetResponse()
'ftpResponse1 = ftpRequest1.GetResponse()
'Dim strstream1 As Stream = ftpResponse1.GetResponseStream()
'Dim strreader1 As New StreamReader(strstream1)
'Console.WriteLine(strreader1.ReadToEnd())
'strreader1.Close()
'strstream1.Close()
'ftpResponse1.Close()
Please help me.
In the above case i am not getting any error but when i am going to upload a rar file then it is giving the following exception
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
And File Upload code is given below
Public Sub FTPUpload(ByVal SourceFile() As IO.FileInfo, ByVal folderLevel As Integer, ByVal ftpPassiveMode As Boolean)
ZXFTPPASS = "******"
Dim filePath As New IO.DirectoryInfo(filePaths)
Dim ftpRequest As FtpWebRequest
Dim dResult As Windows.Forms.DialogResult
Dim ftpFilePath As String = ""
Dim levelPath As String = ""
Dim iLoop As Integer
Dim uFile As IO.FileInfo
For Each uFile In SourceFile
Try
ftpFilePath = levelPath & "/" & uFile.Name
ftpRequest = CType(FtpWebRequest.Create(ftpFilePath), FtpWebRequest)
ftpRequest.Credentials = New NetworkCredential(ZXFTPUSER, ZXFTPPASS)
ftpRequest.UsePassive = ftpPassiveMode
ftpRequest.UseBinary = True
ftpRequest.KeepAlive = False
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
'Read in the file
Dim b_file() As Byte = System.IO.File.ReadAllBytes(filePath.FullName & "\" & uFile.Name.ToString())
'Upload the file
Dim cls_stream As Stream = ftpRequest.GetRequestStream()
cls_stream.Write(b_file, 0, b_file.Length)
cls_stream.Close()
cls_stream.Dispose()
'MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
Catch
MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
End Try
Next
End Sub
Looking through various sites I have found this:
Private Function FtpFolderCreate(folder_name As String, username As String, password As String) As Boolean
Dim request As Net.FtpWebRequest = CType(FtpWebRequest.Create(folder_name), FtpWebRequest)
request.Credentials = New NetworkCredential(username, password)
request.Method = WebRequestMethods.Ftp.MakeDirectory
Try
Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
' Folder created
End Using
Catch ex As WebException
Dim response As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
' an error occurred
If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
Return False
End If
End Try
Return True
End Function
this is working code on how to create a directory on a FTP server Via Vb.net
Dim folderName As String = "POP/" & Date.Now.Year.ToString & Date.Now.Month.ToString.PadLeft(2, "0"c) & Date.Now.Day.ToString.PadLeft(2, "0"c)
Dim RequestFolderCreate As Net.FtpWebRequest = CType(FtpWebRequest.Create("ftp://" & server & "/" & folderName), FtpWebRequest)
RequestFolderCreate.Credentials = New NetworkCredential(username, password)
RequestFolderCreate.Method = WebRequestMethods.Ftp.MakeDirectory
Try
Using response As FtpWebResponse = DirectCast(RequestFolderCreate.GetResponse(), FtpWebResponse)
End Using
Catch ex As Exception//catch a expection

Using FTP to download each file *WHILE* getting the file list

We need to get about 100 very small files from a remote FTP server using vb.net.
Our company won't let us buy (or install) any 3rd party ftp libraries... so we are forced to use something like FtpWebRequest. (Or is there a better free, choice that is already a part of Visual Studio?)
This method works, but it is VERY slow. (I assume because of the constant logging in/out.)
Log in with user name and password.
Get a file-list from the remote server.
Log out
Use that file-list to get each file separtely:
Log in, get the file, log out.
Log in 99 more times, get each file, log out each time.
Instead, we probably should be doing this, but it never works:
Log in with user name and password. ONCE.
Get a list of filenames.
Download each file.
Log out ONCE.
We found COUNTLESS examples online of "getting an FTP file list" and later "how to download 1 file with FTP"... but we never see "get EACH file name, and download it NOW".
Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpSite)
fwr.Credentials = New NetworkCredential(userName, password)
fwr.KeepAlive = True
fwr.Method = WebRequestMethods.Ftp.ListDirectory
Dim sr As IO.StreamReader = Nothing
Try
sr = New IO.StreamReader(fwr.GetResponse().GetResponseStream())
Do Until (sr.EndOfStream())
fileName = sr.ReadLine()
fwr.Method = WebRequestMethods.Ftp.DownloadFile
output = ""
Dim sr2 As IO.StreamReader = Nothing
Try
sr2 = New IO.StreamReader(fwr.GetResponse().GetResponseStream())
output = sr2.ReadToEnd()
Catch ex As Exception
End Try
If (Not sr2 Is Nothing) Then sr2.Close() : sr2 = Nothing
Call MsgBox("Got " & fileName & LF & output)
Loop
Catch ex As Exception
End Try
If (Not sr Is Nothing) Then sr.Close() : sr = Nothing
If (Not fwr Is Nothing) Then fwr = Nothing
Take a look at my FTP class, it might be exactly what you need.
Public Class FTP
'-------------------------[BroCode]--------------------------
'----------------------------FTP-----------------------------
Private _credentials As System.Net.NetworkCredential
Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
setCredentials(_FTPUser, _FTPPass)
End Sub
Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
_FtpWebRequest.Credentials = _credentials
_FtpWebRequest.KeepAlive = False
_FtpWebRequest.Timeout = 20000
_FtpWebRequest.Method = System.Net.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, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
responseStream.CopyTo(fs)
responseStream.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
Dim ret As New List(Of String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
Dim FileData As String = _reader.ReadToEnd
Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
For Each l As String In Lines
ret.Add(l)
Next
_reader.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return ret
End Function
Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
_credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
End Sub
End Class
To initialize:
Dim ftp As New FORM.FTP("username", "password")
ftp.UploadFile("c:\file.jpeg", "ftp://domain/file.jpeg")
ftp.DownloadFile("c:\file.jpeg", "ftp://ftp://domain/file.jpeg")
Dim directory As List(Of String) = ftp.GetDirectory("ftp://ftp.domain.net/")
ListBox1.Items.Clear()
For Each item As String In directory
ListBox1.Items.Add(item)
Next
Something I just put together, the important part is the fwr.Proxy = Nothing, otherwise it tries to auto get the proxy settings which causes huge delays so setting it to nothing forces it to not use one.
If you are using a proxy obviously you need to set this to an actual proxy.
Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress)
fwr.Credentials = New NetworkCredential(userName, password)
fwr.KeepAlive = True
fwr.Method = WebRequestMethods.Ftp.ListDirectory
fwr.Proxy = Nothing
Try
Dim sr As New IO.StreamReader(fwr.GetResponse().GetResponseStream())
Dim lst = sr.ReadToEnd().Split(vbNewLine)
For Each file As String In lst
file = file.Trim() 'remove any whitespace
If file = ".." OrElse file = "." Then Continue For
Dim fwr2 As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress & file)
fwr2.Credentials = fwr.Credentials
fwr2.KeepAlive = True
fwr2.Method = WebRequestMethods.Ftp.DownloadFile
fwr2.Proxy = Nothing
Dim fileSR As New IO.StreamReader(fwr2.GetResponse().GetResponseStream())
Dim fileData = fileSR.ReadToEnd()
fileSR.Close()
Next
sr.Close()
Catch ex As Exception
End Try
I know its a bit late but hopefully helps