使用vb.net进行ftp文件传输而不使用任何第三方工具

时间:2012-02-23 08:39:47

标签: vb.net ftp ftpwebrequest

我正在使用vb.net编写代码,用于从远程计算机到本地计算机的文件传输,而不使用任何第三方工具

这是我的代码

Dim reqFTP As FtpWebRequest
    Dim filepath As String
    Dim filename As String
    Dim filename1 As String
    Dim ftpserverip As String
    Dim ftpuserid As String
    Dim ftpPassword As String
    Try
        filename1 = TxtRemoteFile.Text
        filepath = TxtLocalFile.Text
        filename = Locfname.Text
        ftpserverip = TxtServerIP.Text
        ftpuserid = TxtUserName.Text
        ftpPassword = TxtPwd.Text
        Dim outputStream As FileStream = New FileStream((filepath + ("\\" + filename)), FileMode.Create)
        reqFTP = CType(FtpWebRequest.Create(New Uri(("ftp://" _
                            + (ftpserverip + ("/" + filename1))))), FtpWebRequest)
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
        reqFTP.UseBinary = True
        reqFTP.Credentials = New NetworkCredential(ftpuserid, ftpPassword)
        Dim response As FtpWebResponse = CType(reqFTP.GetResponse, FtpWebResponse)

        outputStream.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

但是收到的错误就像“远程服务器返回错误:(550)fi

1 个答案:

答案 0 :(得分:0)

我有同样的问题。我没有在远程路径中包含httpdocs。 例: ftp://ftp.websitename.com/httpdocs/filenametocopy.txt

System.Net.WebRequest.Create("ftp://ftp.websitename.com/httpdocs/filenametocopy.txt")

权限被拒绝,因为我试图将文件写在根目录之外。