SuperShinta
Member
- Joined
- Oct 8, 2011
- Messages
- 12
- Programming Experience
- Beginner
Imports System.Net
Imports System.IO
Public Class Form4
Private Sub listFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim reader As StreamReader = Nothing
Try
requ = CType(WebRequest.Create(URL), WebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.ListDirectory
resp = CType(requ.GetResponse(), FtpWebResponse)
reader = New StreamReader(resp.GetResponseStream())
While (reader.Peek() > -1)
lstFileList.Items.Add(reader.ReadLine())
End While
ToolStripStatusLabel1.Text = "Listing complete!"
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Finally
If reader IsNot Nothing Then reader.Close()
End Try
End Sub
Private Sub downloadFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim respStrm As Stream = Nothing
Dim fileStrm As FileStream = Nothing
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.DownloadFile
resp = CType(requ.GetResponse(), FtpWebResponse)
respStrm = resp.GetResponseStream()
SaveFileDialog1.FileName = Path.GetFileName(requ.RequestUri.LocalPath)
If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fileStrm = File.Create(SaveFileDialog1.FileName)
Dim buff(1024) As Byte
Dim bytesRead As Integer = 0
While (True)
bytesRead = respStrm.Read(buff, 0, buff.Length)
If (bytesRead = 0) Then Exit While
fileStrm.Write(buff, 0, bytesRead)
End While
ToolStripStatusLabel1.Text = "Download complete!"
End If
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Catch ex As IOException
ToolStripStatusLabel2.Text = ex.Message
Finally
If respStrm IsNot Nothing Then respStrm.Close()
If fileStrm IsNot Nothing Then fileStrm.Close()
End Try
End Sub
Private Sub cmdList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdList.Click
lstFileList.Items.Clear()
listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
End Sub
Private Sub cmdDowload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
downloadFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)
End Sub
Private Sub lstFileList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFileList.SelectedIndexChanged
TextBox1.Text = txturl.Text & "/" & lstFileList.SelectedItems(0).ToString()
End Sub
Private Sub deleteFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.DeleteFile
resp = CType(requ.GetResponse(), FtpWebResponse)
ToolStripStatusLabel1.Text = "File deleted!"
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Finally
If resp IsNot Nothing Then resp.Close()
End Try
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
If (MessageBox.Show("Are you sure you want to delete this file?", "Confirm File Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
deleteFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)
End If
End Sub
Private Sub cmdref_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdref.Click
lstFileList.Items.Clear()
listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
End Sub
End Class
Thanks in advance
Imports System.IO
Public Class Form4
Private Sub listFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim reader As StreamReader = Nothing
Try
requ = CType(WebRequest.Create(URL), WebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.ListDirectory
resp = CType(requ.GetResponse(), FtpWebResponse)
reader = New StreamReader(resp.GetResponseStream())
While (reader.Peek() > -1)
lstFileList.Items.Add(reader.ReadLine())
End While
ToolStripStatusLabel1.Text = "Listing complete!"
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Finally
If reader IsNot Nothing Then reader.Close()
End Try
End Sub
Private Sub downloadFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim respStrm As Stream = Nothing
Dim fileStrm As FileStream = Nothing
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.DownloadFile
resp = CType(requ.GetResponse(), FtpWebResponse)
respStrm = resp.GetResponseStream()
SaveFileDialog1.FileName = Path.GetFileName(requ.RequestUri.LocalPath)
If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fileStrm = File.Create(SaveFileDialog1.FileName)
Dim buff(1024) As Byte
Dim bytesRead As Integer = 0
While (True)
bytesRead = respStrm.Read(buff, 0, buff.Length)
If (bytesRead = 0) Then Exit While
fileStrm.Write(buff, 0, bytesRead)
End While
ToolStripStatusLabel1.Text = "Download complete!"
End If
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Catch ex As IOException
ToolStripStatusLabel2.Text = ex.Message
Finally
If respStrm IsNot Nothing Then respStrm.Close()
If fileStrm IsNot Nothing Then fileStrm.Close()
End Try
End Sub
Private Sub cmdList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdList.Click
lstFileList.Items.Clear()
listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
End Sub
Private Sub cmdDowload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
downloadFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)
End Sub
Private Sub lstFileList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFileList.SelectedIndexChanged
TextBox1.Text = txturl.Text & "/" & lstFileList.SelectedItems(0).ToString()
End Sub
Private Sub deleteFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.DeleteFile
resp = CType(requ.GetResponse(), FtpWebResponse)
ToolStripStatusLabel1.Text = "File deleted!"
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Finally
If resp IsNot Nothing Then resp.Close()
End Try
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
If (MessageBox.Show("Are you sure you want to delete this file?", "Confirm File Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
deleteFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)
End If
End Sub
Private Sub cmdref_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdref.Click
lstFileList.Items.Clear()
listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
End Sub
End Class
Thanks in advance