0
ローカルコンピュータに1分ごとにサーバーから新しいアップロードファイルをダウンロードするためのvb.net Windowsサービスを作成するのに役立つ必要があります。このコードは、サービスが開始されても同じファイルをダウンロードしますが、タイマーはここでは機能しません。1時間ごとにサーバーから新しいアップロードファイルをダウンロードするvb.net Windowsサービスを作成する
サーバにアップロードされた新しいファイルを検出してダウンロードし、毎分タイマーが新しいファイルを定期的にチェックする必要があります。以下は私のコードです。親切に私を助けてください。
コード:
Imports System
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Threading.Timer
Public Class Service1
Dim MyThread As Threading.Thread
Protected Overrides Sub OnStart(ByVal args() As String)
Try
Timer2.Enabled = True
MyThread = New Threading.Thread(AddressOf FTPDownloadFile)
MyThread.Start()
AddHandler Timer2.Tick, AddressOf Timer2_Tick
Timer2.Interval = 60000 ' 1 minute time interval
Timer2.Start()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Timer2.Enabled = False
MyThread.Abort()
End Sub
Private Sub FTPDownloadFile()
Dim ftpuri As String = ""
Dim downloadpath As String = ""
Dim ftpusername As String = ""
Dim ftppassword As String = ""
'Create a WebClient.
Dim request As New WebClient()
' Confirm the Network credentials based on the user name and password passed in.
request.Credentials = New NetworkCredential("abc", "1234")
'Read the file data into a Byte array
Dim bytes() As Byte = request.DownloadData("http://iba-iet.netai.net/wordpress/home-2")
Try
' Create a FileStream to read the file into
Dim DownloadStream As FileStream = IO.File.Create("C:\file.html")
' Stream this data into the file
DownloadStream.Write(bytes, 0, bytes.Length)
' Close the FileStream
DownloadStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
MsgBox("Process Complete")
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Try
FTPDownloadFile()
Timer2.Stop()
Catch ex As Exception
Finally
Timer2.Start()
End Try
End Sub
End Class
おかげ@Kashif。あなたは親切にサーバーからvb.netのWindowsサービスを通してファイルを定期的にダウンロードするべきであることを理解することができます – user2206637
現在のコードは** FTPDownloadFile()**関数を10秒ごとに呼び出すように設定されています '_oTimer.Interval = 10000' –
** FTPDownloadFile()** Subのコードを追加して、フォルダに複数のファイルをダウンロードしてください。 –