How to download each new latest files from server to local pc using vb.net windows service

  • Thread starter Thread starter aftabpalh
  • Start date Start date
A

aftabpalh

Guest
I need help to create a vb.net windows service to download each latest new uploaded files from server to local computer. This code works fine to download only single file once the service started BUT I want to window service detect the each latest new file uploaded on server and download it. Below is my code. Kindly help me.

Points:

1. I want to download every new file which is uploaded on server

2. Each File name should be different

Imports System
Imports System.IO
Imports System.Net
Imports System.Timers
Imports System.Threading

Public Class Service1


Dim timerSchedule As System.Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)
MyThread = New Threading.Thread(AddressOf Execute)

timerSchedule = New System.Timers.Timer(1000)
AddHandler timerSchedule.Elapsed, AddressOf timerSchedule_Elapsed
timerSchedule.Start()

End Sub


Protected Overrides Sub OnStop()

End Sub

Private Sub timerSchedule_Elapsed(ByVal pSender As Object, ByVal pArgs As System.Timers.ElapsedEventArgs)
Try
timerSchedule.Stop()
call my a function to do the scheduled task

FTPDownloadFile()
Catch ex As Exception
Finally
timerSchedule.Start()
End Try
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("root", "")

Read the file data into a Byte array

server path of the file
Dim bytes() As Byte = request.DownloadData("http://localhost/salary_system/home.php")



Try
Create a FileStream to read the file into
For Each i As String In downloadpath


local pc path to store file in directory
Dim DownloadStream As FileStream = IO.File.Create("C:\download\home.php")
Stream this data into the file
DownloadStream.Write(bytes, 0, bytes.Length)
Close the FileStream
DownloadStream.Close()
Next

Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try

MsgBox("Process Complete")

End Sub

Continue reading...
 
Back
Top