woah, einmal durch den webconverter jagen und fertig...
1. Imports System
2. Imports System.Net
3. Imports System.Threading
4. Imports System.IO
5. Imports System.Diagnostics
6. Imports System.Windows.Forms
7.
8. Namespace CreateFdXml
9. Public Class FtpState
10. Private wait As ManualResetEvent
11. Private m_request As FtpWebRequest
12. Private m_fileName As String
13. Private m_operationException As Exception = Nothing
14. Private status As String
15.
16. Public Sub New()
17. wait = New ManualResetEvent(False)
18. End Sub
19.
20. Public ReadOnly Property OperationComplete() As ManualResetEvent
21. Get
22. Return wait
23. End Get
24. End Property
25.
26. Public Property Request() As FtpWebRequest
27. Get
28. Return m_request
29. End Get
30. Set(ByVal value As FtpWebRequest)
31. m_request = value
32. End Set
33. End Property
34.
35. Public Property FileName() As String
36. Get
37. Return m_fileName
38. End Get
39. Set(ByVal value As String)
40. m_fileName = value
41. End Set
42. End Property
43. Public Property OperationException() As Exception
44. Get
45. Return m_operationException
46. End Get
47. Set(ByVal value As Exception)
48. m_operationException = value
49. End Set
50. End Property
51. Public Property StatusDescription() As String
52. Get
53. Return status
54. End Get
55. Set(ByVal value As String)
56. status = value
57. End Set
58. End Property
59. End Class
60.
61. Public Class AsynchronousFtpUpLoader
62. Shared LblStatus As Label
63. Shared Progress As ProgressBar
64. Public Delegate Sub UpdateTextCallback(ByVal text As String)
65. Public Delegate Sub UpdateProgressCallback(ByVal bytesRead As Long, ByVal bytesMaximum As Long)
66.
67. ''' <summary>Length of file to upload</summary>
68. Shared fileLength As Long
69.
70. Public Shared Sub UpdateText(ByVal text As String)
71. LblStatus.Text = text
72. End Sub
73.
74. Public Shared Sub UpdateProgress(ByVal bytesRead As Long, ByVal bytesMaximum As Long)
75. Progress.Value = CInt((bytesRead * (CDbl(100) / CDbl(bytesMaximum))))
76. End Sub
77.
78.
79.
80.
81. ''' <summary> FTP-Upload einer Datei vom lokalen Client zum Server </summary>
82. ''' <param name="target">Ziel-URI. Zum Beispiel: new Uri("ftp://www.name.net/httpdocs/" + fileName)</param>
83. ''' <param name="fileName">Der komplette Pfad der Datei auf der lokalen Maschine</param>
84. ''' <param name="username">Benutzername für die FTP-Verbindung</param>
85. ''' <param name="password">Passwort für die FTP-Verbindung</param>
86. ''' <param name="lblStatus">Eine Instanz eines Label's in den bei einem Upload-Fortschritt
87. ''' als Beispiel ein "2048 / 20454" in die Text-Eigenschaft schreibt</param>
88. ''' Die Instanz darf auch null sein, wobei dann diese Funktionalität deaktiviert ist.</param>
89. ''' <param name="progress">Eine ProgressBar-Instanz, die beim Upload den Fortschritt anzeigt.
90. ''' Die Instanz darf auch null sein, wobei dann diese Funktionalität deaktiviert ist.</param>
91. Public Shared Sub Upload(ByVal target As Uri, ByVal fileName As String, ByVal username As String, ByVal password As String, ByVal lblStatus__1 As Label, ByVal progress__2 As ProgressBar)
92. LblStatus = lblStatus__1
93. fileLength = New FileInfo(fileName).Length
94. If LblStatus IsNot Nothing Then
95. LblStatus.Invoke(New UpdateTextCallback(UpdateText), New Object() {"upload ... / " & fileLength.ToString()})
96. End If
97.
98. Progress = progress__2
99. If Progress IsNot Nothing Then
100. Progress.[Step] = 1
101. Progress.Minimum = 0
102. Progress.Maximum = 100
103. Progress.Visible = True
104. Progress.Invoke(New UpdateProgressCallback(UpdateProgress), New Object() {5, fileLength})
105. End If
106.
107. ' Create a Uri instance with the specified URI string.
108. ' If the URI is not correctly formed, the Uri constructor will throw an exception.
109. Dim waitObject As ManualResetEvent
110.
111. Dim state As New FtpState()
112. Dim request As FtpWebRequest = DirectCast(WebRequest.Create(target), FtpWebRequest)
113. request.Method = WebRequestMethods.Ftp.UploadFile
114.
115. ' This example uses anonymous logon.
116. ' The request is anonymous by default; the credential does not have to be specified.
117. ' The example specifies the credential only to control how actions are logged on the server.
118. request.Credentials = New NetworkCredential(username, password)
119.
120. ' Store the request in the object that we pass into the asynchronous operations.
121. state.Request = request
122. state.FileName = fileName
123.
124. ' Get the event to wait on.
125. waitObject = state.OperationComplete
126.
127. ' Asynchronously get the stream for the file contents.
128. request.BeginGetRequestStream(New AsyncCallback(EndGetStreamCallback), state)
129.
130. Dim done As Boolean = False
131. Do
132. done = waitObject.WaitOne(200, True)
133. Application.DoEvents()
134. Loop While Not done
135.
136. ' The operations either completed or threw an exception.
137. If state.OperationException IsNot Nothing Then
138. Throw state.OperationException
139. End If
140. End Sub
141.
142. Private Shared Sub EndGetStreamCallback(ByVal ar As IAsyncResult)
143. Dim state As FtpState = DirectCast(ar.AsyncState, FtpState)
144.
145. Dim requestStream As Stream = Nothing
146. ' End the asynchronous call to get the request stream.
147. Try
148. requestStream = state.Request.EndGetRequestStream(ar)
149. ' Copy the file contents to the request stream.
150. Const bufferLength As Integer = 2048
151. Dim buffer As Byte() = New Byte(bufferLength - 1) {}
152. Dim count As Integer = 0
153. Dim readBytes As Integer = 0
154. Dim stream As FileStream = File.OpenRead(state.FileName)
155. Do
156. readBytes = stream.Read(buffer, 0, bufferLength)
157. requestStream.Write(buffer, 0, readBytes)
158. count += readBytes
159. If LblStatus IsNot Nothing Then
160. LblStatus.Invoke(New UpdateTextCallback(UpdateText), New Object() {(count.ToString() & " / ") + fileLength.ToString()})
161. End If
162. If Progress IsNot Nothing Then
163. Progress.Invoke(New UpdateProgressCallback(UpdateProgress), New Object() {CLng(count), fileLength})
164. End If
165. Loop While readBytes <> 0
166.
167. ' IMPORTANT: Close the request stream before sending the request.
168. requestStream.Close()
169. ' Asynchronously get the response to the upload request.
170. state.Request.BeginGetResponse(New AsyncCallback(EndGetResponseCallback), state)
171. Catch e As Exception
172. ' Return exceptions to the main application thread.
173. 'AsynchronousFtpUpLoader.txtStatus.AppendText("Could not get the request stream.");
174. state.OperationException = e
175. state.OperationComplete.[Set]()
176. Exit Sub
177. End Try
178. End Sub
179.
180. ' The EndGetResponseCallback method completes a call to BeginGetResponse.
181. Private Shared Sub EndGetResponseCallback(ByVal ar As IAsyncResult)
182. Dim state As FtpState = DirectCast(ar.AsyncState, FtpState)
183. Dim response As FtpWebResponse = Nothing
184. Try
185. response = DirectCast(state.Request.EndGetResponse(ar), FtpWebResponse)
186. response.Close()
187. state.StatusDescription = response.StatusDescription
188. ' Signal the main application thread that the operation is complete.
189. state.OperationComplete.[Set]()
190. Catch e As Exception
191. ' Return exceptions to the main application thread.
192. LblStatus.Invoke(New UpdateTextCallback(UpdateText), New Object() {e.Message})
193. state.OperationException = e
194. state.OperationComplete.[Set]()
195. End Try
196. End Sub
197. End Class
198. End Namespace