' ของผมใช้ .Net 2.0
' Imports System.IO
' Imports System.Net
' pPath คือ Path ของ server ที่เราจะเอาไฟล์ไปวาง
Private Function Upload(ByVal pPath As String) As Boolean
Const BUFFER_SIZE As Integer = 4096
Me.pgbUpload.Value = 0
Dim request As FtpWebRequest
Dim response As FtpWebResponse
Dim sourceStream As Stream = New MemoryStream
Dim requestStream As Stream = sourceStream
Try
request = CType(WebRequest.Create(pPath), FtpWebRequest)
request.UseBinary = True
request.KeepAlive = False
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(Me.txtUserFtp.Text.Trim, Me.txtPassFtp.Text.Trim)
If _HaveEXE Then
If WorkType = UploadType.IsExe Then
sourceStream = New FileStream(Me.txtEXEPath.Text.Trim, FileMode.Open)
End If
End If
If _HaveDLL Then
If WorkType = UploadType.IsDll Then
sourceStream = New FileStream(Me.txtDLLPath.Text.Trim, FileMode.Open)
End If
End If
If _HaveRepTH Then
If WorkType = UploadType.IsRepTH Then
sourceStream = New FileStream(Me.txtRepTHPath.Text.Trim, FileMode.Open)
End If
End If
If _HaveRepEN Then
If WorkType = UploadType.IsRepEN Then
sourceStream = New FileStream(Me.txtRepENPath.Text.Trim, FileMode.Open)
End If
End If
If _WorkType = UploadType.IsGenLog Then
sourceStream = New FileStream(Application.StartupPath & "\Server_Version.xml", FileMode.Open)
End If
requestStream = request.GetRequestStream()
request.ContentLength = sourceStream.Length
Me.pgbUpload.Maximum = sourceStream.Length
Dim buffer(BUFFER_SIZE) As Byte
Dim bytesRead As Integer = sourceStream.Read(buffer, 0, BUFFER_SIZE)
Do
Application.DoEvents()
requestStream.Write(buffer, 0, bytesRead)
bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE)
Try
pgbUpload.Value += bytesRead
Catch ex As Exception
End Try
Loop While (bytesRead > 0)
Me.pgbUpload.Value = Me.pgbUpload.Maximum
sourceStream.Close()
requestStream.Close()
response = CType(request.GetResponse, FtpWebResponse)
Catch ex As Exception
MessageBox.Show(ex.GetType().Name + ": \n" + ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
Return True
End Function