user1648225
user1648225

Reputation: 69

Move file in Windows-Service

This is my first time making a windows service app. I'm trying to move files from one folder to another using a windows service app. It'll do so every 10 seconds. This is the code I'm using. It works when I use it on a windows form app but doesn't work when I use it on a windows-service app.

The code in the Timer1_Tick works if I use it in OnStart. But doesn't work in the timer.

    Protected Overrides Sub OnStart(ByVal args() As String)
        Timer1.Enabled = True
    End Sub

    Protected Overrides Sub OnStop()
        Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim FileToMove As String
        Dim MoveLocation As String
        Dim count1 As Integer = 0
        Dim files() As String = Directory.GetFiles("C:\Documents and Settings\dmmc.operation\Desktop\Q8")
        Dim pdfFiles(100) As String

        For i = 0 To files.Length - 1
            If Path.GetExtension(files(i)) = ".pdf" Then
                pdfFiles(count1) = files(i)
                count1 += 1
            End If
        Next

        For i = 0 To pdfFiles.Length - 1
            If pdfFiles(i) <> "" Then
                FileToMove = pdfFiles(i)
                MoveLocation = "C:\Documents and Settings\dmmc.operation\Desktop\Output\" + Path.GetFileName(pdfFiles(i))
                If File.Exists(FileToMove) = True Then
                    File.Move(FileToMove, MoveLocation)
                End If
            End If
        Next

    End Sub

Upvotes: 1

Views: 2211

Answers (2)

user2163049
user2163049

Reputation: 411

I've also build a service that moves files that are dropped into a folder but I'm using the FileSystemWatcher. The FileSystemWatcher allows me to move the file when a new one is created, create an entry in a SQL Database and send an email notification when it's all done.

This is how I have setup the FileSystemWatcher

  1. Set a folder to watch: watcher.Path = "C:\Folder to Watch"
  2. Set the file type to watch: watcher.Filter = "*.pdf".
  3. The type of event to watch and the Method that gets triggered: watcher.Created += new FileSystemEventHandler(OnChanged);
  4. Lastly, I need to enable the event: watcher.EnableRaisingEvents = true;

Unfortunately, I have instances on a daily basis where the files do not get moved successfully. I get IO exceptions for file in use. The files get copied but the file in the destination folder is 0kb.

I'm trying to troubleshoot it and I've manage to debug remotely but I still haven't figured out what I am doing wrong.

The most common error I get is: Error: System.IO.IOException: The process cannot access the file 'fileName.pdf' because it is being used by another process.

this error does not make sense as the file did not exist prior to my service trying to move it...

Any further help would be appreciated.

Upvotes: 0

Robert Beaubien
Robert Beaubien

Reputation: 3156

Windows.Forms.Timer won't work without a Form instantiated. You should be using System.Timers.Timer instead:

Private WithEvents m_timer As System.Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)
    m_timer = New System.Timers.Timer(1000)   ' 1 second
    m_timer.Enabled = True
End Sub

Private Sub m_timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_timer.Elapsed
    m_timer.Enabled = False

    'Do your stuff here

    m_timer.Enabled = True
End Sub

Upvotes: 1

Related Questions