Pradip Kr. Shukla
Pradip Kr. Shukla

Reputation: 43

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

Upvotes: 2

Views: 14590

Answers (2)

Gerald Leesmann
Gerald Leesmann

Reputation: 29

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

Upvotes: 2

Lee
Lee

Reputation: 61

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

Upvotes: 6

Related Questions