Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

Could someone assist me with the creation of a windows service in VB.net 2008 to copy a text file from one location to another location?

The source location will have one or more text files. If there are no files there then do nothing, otherwise move the file to teh new location specified.

I have following this site http://www.codeguru.com/vb/gen/vb_system/services/article.php/c4825/Creating-a-Windows-Service-in-VBNET.htm[^]

The whole code in the windows service is as follows:
Imports System
Imports System.IO
Imports Microsoft.VisualBasic.FileIO

Public Class Service1

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Timer1.Enabled = True
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Timer1.Enabled = False
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim diInward As DirectoryInfo
Dim fiInward As FileInfo()
Dim intInFiles As Integer = 0
'Checks for all the file in the location where code is IMF
Dim filename As String = Nothing
Dim di As New DirectoryInfo("C:\WFC_INWARD")
Dim fiArr As FileInfo() = di.GetFiles()

'1. Check for inward source files
diInward = New DirectoryInfo("C:\WFC_INWARD")
fiInward = diInward.GetFiles("*.*")
intInFiles = fiInward.Length

If intInFiles > 0 Then
For Each disfri In fiArr
Dim getfilename As String = Nothing
getfilename = Path.GetFileName(disfri.Name) 'eg. WFCInwardfile.bsp
File.Move("C:\WFC_INWARD" & "\" & getfilename, "C:\WFC_INWARD\ServiceTest" & "\" & getfilename) 'move the processed file to the processed location
Next
Else
Exit Sub
End If
End Sub
End Class


I have installed the service and it shows as running but nothing is happening, I mean the test text file in the source location is still there and is not being copied to the destination location. Please advise what I should do to get this working.

Thanking you all in advace.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jul-13 21:14pm    
Why? why?!
—SA
ekipongi 8-Jul-13 21:20pm    
Hi Sergey Alexandrovich Kryukov, This is just a test to see if this can work in Windows Service as I'm working on a project to check if files exist in a location and if it does the contents will be copied while adding new header and footer lines to a new text file which will then be copied over to the processed location. So i posted the above to seek assistance in creating the service to copy the file for one location to another first.
[no name] 8-Jul-13 21:26pm    
There is a place to ask the author about his code at the bottom of that article.... Besides that, I do not see where you are doing any error checking or logging exceptions.
ekipongi 8-Jul-13 21:35pm    
ThePhantomUpvoter, the code I posted don't have any error as is confirmed with a simple windows form application. It's working fine in the Windows form apps but I just need a windows service for this so asked. How will you do debugging to check for errors when creating a windows service if that is what you meant?
[no name] 8-Jul-13 22:22pm    
It might work "fine" in a winform application but a service application is not a winform application is it? And, had you read my comment, I do error checking and logging in my services so I know what is going instead of trying to guess.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900