vb.net - 如何在FTP服务器上创建目录

标签 vb.net

我正在使用以下代码在 FTP 服务器上创建文件夹;但它不适用于我的情况:-

            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()

请帮助我。

在上面的情况下,我没有收到任何错误,但是当我要上传 rar 文件时,它会给出以下异常

远程服务器返回错误:(550) 文件不可用(例如,未找到文件、无法访问)。

文件上传代码如下

 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

最佳答案

浏览各个网站我发现了这一点:

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

关于vb.net - 如何在FTP服务器上创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031336/

相关文章:

vb.net - 以编程方式切换 DataGridView 中的 CheckBox 单元格

vb.net - 如何从 VB.NET 中的多变量函数返回一元函数

vb.net - 使用 SSML 音素元素

sql-server - 如果数据为 null,XmlNode.InnerText 将中断

mysql - SQL QUERY <未知字段列表> 中的未知列 <未知列名>

javascript - 如何从 vb.net 代码调用 javascript 函数?

java - 页面加载后才会显示正在加载的图像。

c# - MVC URL.RouteUrl 删除 RouteValues 但 URL.Action 保留它们?

asp.net/vb.net System.Web.UI.WebControls.RepeaterItemEventArgs 问题

.net - VB.net 从具有多列的 ListViews 中读取