Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Form7
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
'Create Request
Dim Request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://testup/library/" + cmbMc.SelectedText + txtBom.Text), System.Net.FtpWebRequest)
'Upload Properties
Request.Credentials = New System.Net.NetworkCredential("test", "123456")
Request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
'Read File
Dim File() As Byte = System.IO.File.ReadAllBytes(txtFile.Text)
'Upload
Dim Strz As System.IO.Stream = Request.GetRequestStream()
Strz.Write(File, 0, File.Length)
Strz.Close()
Strz.Dispose()
MessageBox.Show("Upload complate")
txtLink.Text = "http://testup/saw/library/" + lblFile.Text
Catch ex As Exception
MessageBox.Show(ex.Message, "Messsage")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
txtFile.Text = FolderBrowserDialog1.SelectedPath
End Sub
End Class
Tag : .NET, MySQL, Win (Windows App), VB.NET, VS 2010 (.NET 4.x)
Private Sub MakeDir(ByVal dirName As String)
FTPSettings.IP = "DOMAIN NAME"
FTPSettings.UserID = "USER ID"
FTPSettings.Password = "PASSWORD"
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + dirName)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
ftpStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
Throw New Exception(ex.Message.ToString())
End Try
End Sub
Public NotInheritable Class FTPSettings
Private Sub New()
End Sub
Public Shared Property IP() As String
Get
Return m_IP
End Get
Set(ByVal value As String)
m_IP = Value
End Set
End Property
Private Shared m_IP As String
Public Shared Property UserID() As String
Get
Return m_UserID
End Get
Set(ByVal value As String)
m_UserID = Value
End Set
End Property
Private Shared m_UserID As String
Public Shared Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = Value
End Set
End Property
Private Shared m_Password As String
End Class
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Net
Public NotInheritable Class FTPSettings
Private Sub New()
End Sub
Public Shared Property IP() As String
Get
Return m_IP
End Get
Set(ByVal value As String)
m_IP = Value
End Set
End Property
Private Shared m_IP As String
Public Shared Property UserID() As String
Get
Return m_UserID
End Get
Set(ByVal value As String)
m_UserID = Value
End Set
End Property
Private Shared m_UserID As String
Public Shared Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = Value
End Set
End Property
Private Shared m_Password As String
End Class
Public Class Form7
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
FTPSettings.IP = "Host You"
FTPSettings.UserID = "Username You"
FTPSettings.Password = "Passwosd You"
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + "foder ต่อ(หากมี)" + "/" + "ชื่อที่ต้องการ")), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
ftpStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
MessageBox.Show(ex.Message, "Messsage")
End Try
Try
Dim root As New System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
Dim ufiles() As System.IO.FileInfo = root.GetFiles
For Each ufile As System.IO.FileInfo In ufiles 'File name Show at Listbox
'Create Request
Dim Request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + FTPSettings.IP + "/" + txtBom.Text + "/" + ufile.Name), System.Net.FtpWebRequest)
'Upload Properties
Request.Credentials = New System.Net.NetworkCredential("User name you", "Password you")
Request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
'Read File
Dim File() As Byte = System.IO.File.ReadAllBytes(txtFile.Text + "\" + ufile.Name)
'Upload
Dim Strz As System.IO.Stream = Request.GetRequestStream()
Strz.Write(File, 0, File.Length)
Strz.Close()
Strz.Dispose()
Next
MessageBox.Show("Upload complate")
txtLink.Text = "http://ชื่อ โฮส" + "foder ต่อ(หากมี)" + "/" + "ชื่อfoder ที่ สร้างใหม่"+ "/" 'ลิ้งดาวโหลด
Catch ex As Exception
MessageBox.Show(ex.Message, "Messsage")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PathName As String = Nothing
If (FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then 'Show dialog
PathName = CStr(FolderBrowserDialog1.SelectedPath) 'Get Path Folder
txtFile.Text = PathName
Dim root As New System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath) 'Get Path Folder
txtBom.Text = root.Name
End If
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
End
End Sub
Private Sub btnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancle.Click
txtFile.Text = ""
txtBom.Text = ""
txtLink.Text = ""
txtName.Text = ""
txtSize.Text = ""
End Sub
End Class
พอจะมีการตรวจสอบชื่อไฟล์ บน ftp server หรือเปลาครับ