Das Prinzip ist, eine ASPX-Seite wird aufgerufen und fragt beim Source-Server nach, wie alt eine bestimmte Datei ist.
In diesem Fall eine Access-Datenbank.
Um die Zeit wirklich vergleichen zu können, wird die Zeit seit der letzten Änderung in Sekunden zurückgegeben und mit der lokalen Datei verglichen.
Wenn diese einen Schwellwert erreicht hat, wird die Datei (natürlich aus einem Temp-Directory) heruntergeladen, auf Grundfunktionalität überprüft und ins App_Data verzeichnis kopiert.
Downloadfile.aspx und getDBDate.aspx sind beide nur exemplarisch... der Server existiert nicht mehr und deswefgen gibts auch keinen Code mehr. Aber so ähnlich wie oben sollte es gehen....
1 Imports System.IO
2 Imports System.Data
3
4 Imports System.Threading
5 Imports System.Diagnostics
6
7
8 Partial Class _default
9 Inherits System.Web.UI.Page
10 Dim Logentrys As String = ""
11
12 Dim downloadFilename As String = "Pfaaaaad\mySikDataBaseName.mdb.loading"
13 Dim targetFilename As String = "PfaaaaadWWW3\App_Data\myDataBaseName.mdb"
14 Dim sourceTimeStampURL As String = "http://yourDomaincom/getDBDate.aspx"
15 Dim sourceDownloadURL As String = "http://yourDomaincom/downloadFile.aspx"
16 Dim SikDbFilename As String = "Pfaaaaad\mySikDataBaseName.mdb"
17 Dim SMTPHost As String = "mail.yourDomain.de"
18 Dim EmailTo As String = "xx@yourdoamin.de"
19 Dim EmailFrom As String = "xx@yourdoamin.de"
20 Dim SubjectLine As String = "UpdateReport"
21
22 Dim timeTowait As Integer = 240
23
24 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
25 Exit Sub
26 Server.ScriptTimeout = 600
27 Dim SecondsFromLast As Integer = CInt(getFromURL(sourceTimeStampURL))
28 Dim StartDownload As Boolean = False
29
30 Response.Write("<pre>")
31 If File.Exists(downloadFilename) Then
32 If CDate(File.GetLastWriteTime(downloadFilename)) < Now.AddSeconds(-timeTowait) Then
33 Try
34 Kill(downloadFilename)
35 Catch ex As Exception
36
37 End Try
38 Else
39 WriteEventEntry("<br> Update in Prozess ... next Try:" & CDate(File.GetLastWriteTime(downloadFilename)).AddSeconds(timeTowait))
40 WriteEventEntry("<br> ServerTime:" & Now)
41 Exit Sub
42 End If
43 End If
44 If File.Exists(targetFilename) Then
45 If CDate(File.GetLastWriteTime(targetFilename)) < Now.AddSeconds(-timeTowait) Then
46 Else
47 WriteEventEntry("<br> Update in Prozess .... next Try:" & CDate(File.GetLastWriteTime(targetFilename)).AddSeconds(timeTowait))
48 WriteEventEntry("<br> ServerTime:" & Now)
49 Exit Sub
50 End If
51 End If
52
53
54 If File.Exists(targetFilename) Then
55 Dim x As Date = CDate(File.GetLastWriteTime(targetFilename))
56 Dim SecondsFromLastLocal As Integer = DateDiff(DateInterval.Second, x, Now)
57 Response.Write("<BR> DB Date Strato " & x)
58 Response.Write("<BR> SecondsFromLast SourceServer " & SecondsFromLast)
59 Response.Write("<BR> SecondsFromLast TargetServer " & SecondsFromLastLocal)
60
61 If SecondsFromLast < SecondsFromLastLocal - 200 Then
62 StartDownload = True
63 End If
64 Else
65 StartDownload = True
66 End If
67 If StartDownload Then
68 Response.Write("<BR><BR>StartDownload ")
69 Response.Flush()
70 Application.Lock()
71 WriteEventEntry("")
72 runUpdate()
73 WriteEventEntry("runUpdate finished")
74 Application.UnLock()
75 Dim mail As New System.Net.Mail.MailMessage(EmailFrom, EmailTo, SubjectLine, Logentrys)
76 Dim myClient As New Net.Mail.SmtpClient()
77 myClient.Host = SMTPHost
78 myClient.Send(mail)
79
80 Else
81
82 Response.Write("<BR><BR>No update needed")
83 End If
84 End Sub
85 Function CheckDBFunctionality(ByVal Filename As String) As Boolean
86 WriteEventEntry("CheckDBFunctionality:" & Filename)
87 If Not File.Exists(Filename) Then
88 WriteEventEntry("File.Exists failed:" & Filename)
89 Return False
90 End If
91 Response.Write("<BR> File.Exists(" & Filename & ")")
92 Response.Flush()
93
94 Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Filename & ";Persist Security Info=True"
95 Using connection As New OleDb.OleDbConnection(connectionString)
96
97 WriteEventEntry("connection open")
98 Try
99 Dim ds As New DataSet
100 Dim adapter As New OleDb.OleDbDataAdapter()
101 adapter.SelectCommand = New OleDb.OleDbCommand("Select top(1) * from Anbieter", connection)
102
103 adapter.Fill(ds)
104 WriteEventEntry("Select top(1) * from Anbieter OK")
105 If ds.Tables(0).Rows.Count > 0 Then
106 Response.Write("<BR> CheckDBFunctionality =TRUE")
107 Response.Flush()
108 Return True
109 Else
110 WriteEventEntry("Select top(1) * from Anbieter ds.Tables(0).Rows.Count<=0")
111 End If
112 Catch ex As Exception
113 WriteEventEntry("CheckDBFunctionality failed:" & ex.Message)
114 Response.Write("<BR> CheckDBFunctionality =False" & "<br>" & ex.Message & "<br>" & Filename)
115 Response.Flush()
116 Return False
117 End Try
118
119 End Using
120 End Function
121
122 Public Sub runUpdate()
123 WriteEventEntry("Run Update")
124 Dim wc As New Net.WebClient
125
126 Try
127 Kill(downloadFilename)
128 Catch ex As Exception
129 WriteEventEntry("Kill file failed:" & ex.Message)
130 End Try
131
132
133 wc.DownloadFile(sourceDownloadURL, downloadFilename)
134 WriteEventEntry("DownloadFile complete")
135 If Not CheckDBFunctionality(downloadFilename) Then
136 WriteEventEntry(" Downloadfile corrupt .... Try again ")
137 wc.DownloadFile(sourceDownloadURL, downloadFilename)
138 If Not CheckDBFunctionality(downloadFilename) Then
139 WriteEventEntry(" Downloadfile corrupt second Time .... STOP Process")
140 Exit Sub
141 End If
142 End If
143 WriteEventEntry("CheckDBFunctionality complete")
144
145
146
147 Response.Write("<BR>OK! wc.DownloadFile(""" & sourceDownloadURL & """," & downloadFilename & " )")
148 Response.Flush()
149
150 If Not File.Exists(SikDbFilename) Then
151 Try
152 File.Copy(targetFilename, SikDbFilename)
153
154 Catch ex As Exception
155 Try
156 File.Copy(downloadFilename, SikDbFilename)
157 Catch ex2 As Exception
158 End Try
159 End Try
160 End If
161 If CheckDBFunctionality(downloadFilename) Then
162 If File.Exists(SikDbFilename) Then
163
164 If File.GetLastWriteTime(SikDbFilename) < Now.AddDays(-1) Then
165 Try
166 Kill(SikDbFilename)
167 Catch ex As Exception
168 End Try
169
170 WriteEventEntry("<BR> File.Copy(" & targetFilename & "," & SikDbFilename & " )")
171 Response.Flush()
172 File.Copy(targetFilename, SikDbFilename)
173 End If
174
175 Else
176
177 WriteEventEntry("<BR> File.Copy(" & targetFilename & "," & SikDbFilename & " )")
178 Response.Flush()
179 File.Copy(targetFilename, SikDbFilename)
180
181 End If
182 End If
183
184 If File.Exists(targetFilename) Then
185 For I As Integer = 0 To 100
186 System.Threading.Thread.Sleep(50)
187 Try
188 Kill(targetFilename)
189 WriteEventEntry("Kill TargetFilename Success")
190 Exit For
191 Catch ex As Exception
192 WriteEventEntry("Kill TargetFilename Failed")
193
194 End Try
195 Next
196 End If
197
198 Try
199
200 WriteEventEntry("<BR> File.Copy(" & downloadFilename & "," & targetFilename & " )")
201 Response.Flush()
202 File.Move(downloadFilename, targetFilename)
203 WriteEventEntry(" File.Move Success")
204 Catch ex As Exception
205
206 End Try
207 Dim p As New ProcessInfo
208
209 If Not CheckDBFunctionality(targetFilename) Then
210 If File.Exists(SikDbFilename) Then
211 Try
212 Kill(targetFilename)
213 Catch ex As Exception
214 WriteEventEntry("Kill(targetFilename) " & ex.Message)
215
216 End Try
217 WriteEventEntry("<BR> TargetFile corrupt ... copy SikFile")
218 File.Copy(SikDbFilename, targetFilename)
219 Else
220 WriteEventEntry("<BR> TargetFile corrupt ... !!!!!!!!!!!NO!!!! SikFile")
221 End If
222 End If
223
224
225 WriteEventEntry("Tariftipp update Success", True)
226
227 Response.Flush()
228
229 End Sub
230
231 Public Sub AppendTextFile(ByVal Path As String, ByVal Value As Object, Optional ByVal tmpFilename As Boolean = False)
232 '"d:\Adjustment\DB\restartCache\"
233 Dim f As Integer = FreeFile()
234 FileSystem.FileOpen(f, Path, OpenMode.Append, OpenAccess.Write, OpenShare.Default)
235 FileSystem.PrintLine(f, Value)
236 FileSystem.FileClose(f)
237 End Sub
238 Public Sub WriteEventEntry(ByVal value As String, Optional ByVal priority As Boolean = False)
239 Logentrys &= value & vbCrLf
240 Response.Write(value)
241 value = Replace(value, "<br>", "", , , CompareMethod.Text)
242 AppendTextFile(Left(downloadFilename, InStrRev(downloadFilename, "\")) & "log.txt", Now & " " & value)
243 Response.Flush()
244
245 End Sub 'Main
246
247
248
249 Function getFromURL(ByVal url As String) As String
250 Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
251 'request.Headers.Add("HTTP_USER_AGENT", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322; .NET CLR 2.0.50215)")
252
253 Dim response As System.Net.WebResponse
254
255 response = request.GetResponse()
256
257 Dim responseStream As System.IO.Stream = response.GetResponseStream()
258
259 Dim reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
260
261 Return reader.ReadToEnd()
262
263 End Function
264
265
266 Sub getImageFromURL(ByVal url As String, ByVal PAth As String)
267 Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
268
269 Dim response As System.Net.WebResponse
270
271 response = request.GetResponse()
272
273 Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(response.GetResponseStream)
274 img.Save(PAth)
275
276
277
278 End Sub
279
280
281 End Class