-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp.vb
159 lines (144 loc) · 6.88 KB
/
ftp.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Imports System.Net
Imports System.IO
Public Class ftp
Public Property UseSystemPasswordChar As Boolean
Public Property PasswordChar As Char
Private Sub listFTP(ByVal URL As String, ByVal bk As String, ByVal pw 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(bk, pw)
requ.Method = WebRequestMethods.Ftp.ListDirectory
resp = CType(requ.GetResponse(), FtpWebResponse)
reader = New StreamReader(resp.GetResponseStream())
While (reader.Peek() > -1)
ListBox1.Items.Add(reader.ReadLine())
End While
ToolStripStatusLabel1.Text = "list 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 bk As String, ByVal pw As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim respStrm As Stream = Nothing
Dim fileStrm As FileStream = Nothing
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(bk, pw)
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 uploadFTP(ByVal URl As String, ByVal filename As String, ByVal bk As String, ByVal pw As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim requStrm As Stream = Nothing
Dim fileStrm As FileStream = Nothing
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(bk, pw)
requ.Method = WebRequestMethods.Ftp.UploadFile
requ.Timeout = System.Threading.Timeout.Infinite
requ.Proxy = Nothing
requStrm = requ.GetRequestStream()
Dim buff(2048) As Byte
Dim bytesRead As Integer = 0
fileStrm = File.OpenRead(filename)
Do While (True)
bytesRead = fileStrm.Read(buff, 0, buff.Length)
If (bytesRead = 0) Then Exit Do
requStrm.Write(buff, 0, bytesRead)
Loop
requStrm.Close()
resp = CType(requ.GetResponse(), FtpWebResponse)
ToolStripStatusLabel1.Text = "Upload complete "
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As IOException
ToolStripStatusLabel2.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Finally
If resp IsNot Nothing Then resp.Close()
If fileStrm IsNot Nothing Then fileStrm.Close()
If requStrm IsNot Nothing Then requStrm.Close()
End Try
End Sub
Private Sub deleteFTP(ByVal URL As String, ByVal bk As String, ByVal pw As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(bk, pw)
requ.Method = WebRequestMethods.Ftp.DeleteFile
resp = CType(requ.GetResponse(), FtpWebResponse)
ToolStripStatusLabel1.Text = "The File was 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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
listFTP(txt_server.Text, txt_user.Text, txt_pw.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
downloadFTP(TextBox4.text, txt_user.Text, txt_pw.Text)
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox4.Text = txt_server.Text & "/" & ListBox1.SelectedItems(0).ToString()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
uploadFTP(OpenFileDialog1.FileName, txt_server.Text + "/" + Path.GetFileName(OpenFileDialog1.FileName), txt_user.Text, txt_pw.Text)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If (MessageBox.Show("Do you want realy to delete this file? ", " Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
deleteFTP(TextBox4.Text, txt_user.Text, txt_pw.Text)
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
ListBox1.Items.Clear()
listFTP(txt_server.Text, txt_user.Text, txt_pw.Text)
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
ListBox1.Items.Clear()
End Sub
Private Sub ftp_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txt_pw.TextBox.PasswordChar = "*"c
End Sub
End Class