Imports System.IO
Imports System.Data
Imports System.Windows.Forms
Imports System.Net
Public Class Form2
Public NotInheritable Class FTPSettings
Private Sub New()
End Sub
Public Shared Property IP() As String
Get
Return m_IP
End Get
Set(ByVal value As String)
m_IP = value
End Set
End Property
Private Shared m_IP As String
Public Shared Property UserID() As String
Get
Return m_UserID
End Get
Set(ByVal value As String)
m_UserID = value
End Set
End Property
Private Shared m_UserID As String
Public Shared Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = value
End Set
End Property
Private Shared m_Password As String
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FTPSettings.IP = "host"
FTPSettings.UserID = "user"
FTPSettings.Password = "pass"
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
'Create folder mc
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + "sawlibrary")), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
ftpStream.Close()
response.Close()
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
Exit Sub
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Messsage")
Exit Sub
End Try
End Sub
End Class
Tag : .NET, MySQL, Win (Windows App), VB.NET, VS 2010 (.NET 4.x)
Public Function CheckIfFtpFileExists(ByVal fileUri As String) As Boolean
Dim request As FtpWebRequest = WebRequest.Create(fileUri)
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.GetFileSize
Try
Dim response As FtpWebResponse = request.GetResponse()
' THE FILE EXISTS
Catch ex As WebException
Dim response As FtpWebResponse = ex.Response
If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
' THE FILE DOES NOT EXIST
Return False
End If
End Try
Return True
End Function
Code (VB.NET)
If CheckIfFtpFileExists("ftp://ftp.domain.com/filename.txt") Then
' Do something
End If