Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ServerFTP As String = "ftp://dominio.it/"
Dim User As String = "username"
Dim Pwd As String = "password"
Dim FolderName As String = "beckup20130218"
CreaCartellaFTPServer(FolderName, ServerFTP, User, Pwd)
UploadFile(ServerFTP & FolderName, "c:\temp_img_server.Jpeg", User, Pwd)
End Sub
Private Function UploadFile(ByVal UrlServerFTPByVal As String, ByVal FileUpload As String, ByVal User As String, ByVal Pwd As String) As Boolean
Dim FileRename As String = System.IO.Path.GetFileName(FileUpload)
If UrlServerFTPByVal.Substring(UrlServerFTPByVal.Length - 1, 1) <> "/" Then
UrlServerFTPByVal = UrlServerFTPByVal & "/"
End If
Dim DestFTP As String = UrlServerFTPByVal & FileRename
Try
My.Computer.Network.UploadFile(FileUpload, DestFTP, User, Pwd, False, 100000)
Return True
Catch Errore As Exception
MsgBox(Errore.Message)
Return False
End Try
End Function
Private Sub CreaCartellaFTPServer(ByVal Folder_Name As String, ByVal ServerFTP As String, ByVal UserNameFTP As String, ByVal PasswordFTP As String)
ServerFTP = ServerFTP.Replace("ftp://", "").Replace("/", "")
FTPSettings.IP = ServerFTP
FTPSettings.UserID = UserNameFTP
FTPSettings.Password = PasswordFTP
Dim reqFTP As Net.FtpWebRequest = Nothing
Dim ftpStream As IO.Stream = Nothing
Try
reqFTP = DirectCast(Net.FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + Folder_Name)), Net.FtpWebRequest)
reqFTP.Method = Net.WebRequestMethods.Ftp.MakeDirectory
reqFTP.UseBinary = True
reqFTP.Credentials = New Net.NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
Dim response As Net.FtpWebResponse = DirectCast(reqFTP.GetResponse(), Net.FtpWebResponse)
ftpStream = response.GetResponseStream()
ftpStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
If ftpStream Is Nothing Then
'MsgBox("esiste la cartella")
End If
' Throw New Exception(ex.Message.ToString())
End Try
End Sub
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
End Class