Private Function GetOnlyDirectories(ByVal v_server As String, ByVal v_user As String, ByVal v_pw As String) As String
Dim TheShortList As String = FTPListDirectory(v_server, v_user, v_pw, True)
Dim theDirectories As String = ""
If TheShortList <> "" Then
Dim TheLists() As String = Split(TheShortList.Trim, vbCrLf)
For Each thelist As String In TheLists
If thelist.Trim.ToLower.EndsWith(".xml") Then
Dim v_ffile As String = v_server + thelist.Trim.ToLower
Dim v_tfile As String = "../temp.xml"
If ftpDownload(v_ffile, v_tfile, v_user, v_pw) Then
ftpDelete(v_ffile, v_user, v_pw)
End If
End If
theDirectories &= vbCrLf & thelist.Trim
theDirectories = theDirectories.Trim
Next
End If
Return theDirectories
End Function
Public Shared Function ftpDownload(ByVal source As String, ByVal target As String, ByVal v_fuser As String, ByVal v_fpw As String) As Boolean
Dim v_ret As Boolean = True
Dim credential As New NetworkCredential(v_fuser, v_fpw)
Dim clsrequest As FtpWebRequest = _
DirectCast(WebRequest.Create(source), FtpWebRequest)
clsrequest.Method = WebRequestMethods.Ftp.DownloadFile
clsrequest.Credentials = credential
Dim response As FtpWebResponse = _
DirectCast(clsrequest.GetResponse, FtpWebResponse)
Dim stream As Stream = response.GetResponseStream
Dim reader As New StreamReader(stream)
Dim v_cont As String = reader.ReadToEnd()
If (File.Exists(target)) Then
File.Delete(target)
End If
Dim FS As FileStream = New FileStream(target, FileMode.CreateNew, FileAccess.Write)
Dim SW As StreamWriter = New StreamWriter(FS, Encoding.GetEncoding("utf-8"))
SW.WriteLine(v_cont)
SW.Flush()
FS.Close()
reader.Close()
response.Close()
'Read to dataset
Dim ds As DataSet = New DataSet
Dim FSr As FileStream
FSr = New FileStream(target, FileMode.Open)
Dim XmlRead As New XmlTextReader(FSr)
ds.ReadXml(XmlRead)
FSr.Close()
XmlRead.Close()
'do process of dataset ……
ds.Dispose()
Return v_ret
End Function
Function FTPListDirectory(ByVal Target As String, ByVal UID As String, _
ByVal Pass As String, Optional ByVal Shortlist As Boolean = False) As String
Try
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(Target), _
System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential(UID, Pass)
If Shortlist Then
clsRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory
Else
clsRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
End If
Dim str As String = GetStringResponse(clsRequest)
clsRequest.Abort()
Return str
Catch ex As Exception
Throw New Exception("Connection Problem – Error:" & vbCrLf & vbCrLf & ex.Message)
Return ""
End Try
End Function
Private Function GetStringResponse(ByVal ftp As FtpWebRequest) As String
Dim result As String = ""
Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
Dim size As Long = response.ContentLength
Using datastream As Stream = response.GetResponseStream
Using sr As New StreamReader(datastream)
result = sr.ReadToEnd()
sr.Close()
End Using
datastream.Close()
End Using
response.Close()
End Using
Return result
End Function
Public Shared Function ftpDelete(ByVal source As String, ByVal v_fuser As String, ByVal v_fpw As String) As Boolean
Dim v_ret As Boolean = True
Dim credential As New NetworkCredential(v_fuser, v_fpw)
Dim clsrequest As FtpWebRequest = _
DirectCast(WebRequest.Create(source), FtpWebRequest)
clsrequest.Method = WebRequestMethods.Ftp.DeleteFile
clsrequest.Credentials = credential
Dim response As FtpWebResponse = _
DirectCast(clsrequest.GetResponse, FtpWebResponse)
Dim v_mmm As String = response.StatusDescription
response.Close()
Return v_ret
End Function
沒有留言:
張貼留言