我是VB.NET的新手,我在操作FTP文件时遇到了麻烦。
我需要的是连接服务器的功能,另一个用于列出文件,另一个用于下载文件,另一个用于发送电子邮件。我做了很多研究,但到目前为止还不够。
对于我使用过的连接:
Public Function OpenConnection(ByVal sServer As String, ByVal sUser As String, ByVal sPassword As String) As Boolean
Try
Dim requestAs FtpWebRequest = FtpWebRequest.Create(sServer)
request.Credentials = New NetworkCredential(sUser, sPassword)
fwr.KeepAlive = True
OpenConnection = True
Catch ex As Exception
OpenConnection = False
End Try
End Function
在更改" OpenConnection"之前,如何确认确实已连接?真的吗?
列出我使用的文件:
Public Function GetDirectoryListing(ByVal sFilter As String, ByVal strServidor As String) As cDirList
Try
Dim fwr As FtpWebRequest = FtpWebRequest.Create(strServidor)
fwr.Credentials = New NetworkCredential("user", "psswd")
fwr.Method = WebRequestMethods.Ftp.ListDirectory
Dim sr As New StreamReader(fwr.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
Console.WriteLine(str)
str = sr.ReadLine()
End While
sr.Close()
sr = Nothing
fwr = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
但是通过这个功能,我再次连接并且不想这样做。我想连接一次并保持连接,直到我完成使用FTP服务器。这可能吗?
这也不起作用,这是我得到的错误:
远程服务器返回错误:(530)未登录。
如果我要问的很简单,或者你不明白我想问什么,我很抱歉。正如我之前所说,我是新手,英语不是我的第一语言。
答案 0 :(得分:1)
你是对的,这些都是noob问题。
首先,了解变量的范围。如果你想:
Dim request As FtpWebRequest
..在同一个类中的其他函数中可用,您需要在类级别将其设置为PRIVATE。然后你可以在其他功能中使用它。您也可以将它传递给您的方法,但这需要更多的知识。
第二种方法错误;看看这两行,并告诉我有什么不同(变量名称除外):
request.Credentials = New NetworkCredential(sUser, sPassword)
fwr.Credentials = New NetworkCredential("user", "psswd")
第二个错误是使用“user”作为用户名,“pswd”作为连接FTP服务器的密码。我认为这些是不正确的。
答案 1 :(得分:0)
没有太多理由让一个函数打开连接,只是调用另一个函数来获取目录列表。它们应该是一个功能,因为我不知道检查连接是否实际打开的方法。此外,那就是Try / Catch的用途。
Imports System.IO
Imports System.Net
Private sDirectoryList() As String
'Some method of starting the Connection, Listing, Downloads, etc.
Private Sub btnFTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFTP.Click
Dim sAddress As String = "ftp://address/"
Dim sUserName As String = "user"
Dim sPassword As String = "pass"
Dim sLocalDirectory As String = "C:\"
'If the initial connection opens, continue.
'Although this isn't really necessary. It should be included in the GetDirectoryListing function
If OpenConnection(sAddress, sUserName, sPassword) Then
GetDirectoryListing()
'DownloadAllFiles(sAddress, sUserName, sPassword, sLocalDirectory)
'SendEmail()
MessageBox.Show("Done.")
End If
End Sub
Private Function OpenConnection(ByVal sServer As String, ByVal sUser As String, ByVal sPassword As String) As Boolean
Try
ftpRequest = FtpWebRequest.Create(New Uri(sServer))
ftpRequest.Credentials = New NetworkCredential(sUser, sPassword)
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory 'List, not Download
ftpRequest.KeepAlive = True
ftpRequest.Proxy = Nothing
ftpRequest.UseBinary = True
Return True 'True, all worked
Catch ex As WebException
MessageBox.Show("Failed to connect to the FTP server. OpenConnection()")
'MessageBox.Show(ex.ToString())
Return False
End Try
End Function
Private Sub GetDirectoryListing()
Try
'Read the contents of the directory and store it into a string array
Dim srReader As New StreamReader(ftpRequest.GetResponse().GetResponseStream())
sDirectoryList = srReader.ReadToEnd().Split(vbNewLine)
'Clear the last split vbNewLine
If sDirectoryList IsNot Nothing Then
sDirectoryList(sDirectoryList.Length - 1) = ""
End If
srReader.Close()
srReader = Nothing
ftpRequest = Nothing
Catch ex As WebException
MessageBox.Show("Failed to get directory listing. GetDirectoryListing()")
'MessageBox.Show(ex.ToString())
End Try
End Sub
End Class
Private Function ConnectAndGetList(ByVal sServer As String, ByVal sUser As String, ByVal sPassword As String) As Boolean
Try
ftpRequest = FtpWebRequest.Create(New Uri(sServer))
ftpRequest.Credentials = New NetworkCredential(sUser, sPassword)
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory 'List, not Download
ftpRequest.KeepAlive = True
ftpRequest.Proxy = Nothing
ftpRequest.UseBinary = True
'Read the contents of the directory and store it into a string array
Dim srReader As New StreamReader(ftpRequest.GetResponse().GetResponseStream())
sDirectoryList = srReader.ReadToEnd().Split(vbNewLine)
'Clear the last split vbNewLine
If sDirectoryList IsNot Nothing Then
sDirectoryList(sDirectoryList.Length - 1) = ""
End If
srReader.Close()
srReader = Nothing
ftpRequest = Nothing
Catch ex As WebException
MessageBox.Show("Something failed in ConnectAndGetList()")
'MessageBox.Show(ex.ToString())
End Try
End Sub
如果您只想要* .txt文件:
For Each sFile As String In sDirectoryList
'Clear potential whitespace
sFile = sFile.Trim()
If sFile.EndsWith(".txt") Then
'Read the file...
End If
Next