Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 73.755
  • Views Posts: 551.419
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

VB.NET - COME CREARE UNA CARTELLA SU UN SERVER FTP E COME UPLOAD FILE SUL SERVER FTP (Codice)

VB.NET -  COME CREARE UNA CARTELLA SU UN SERVER FTP E COME UPLOAD FILE SUL SERVER FTP (Codice)


 

Oggi vi voglio mettere a disposizione due funzioni una che Crea una cartella nel server FTP e l'altra Upload i file nel server FTP. 

CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET

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


 

By ImaginSystems & Queen Gin 
Categoria: VB.NET
lunedì, 25 feb 2013 Ore. 16.03

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003