Link to home
Start Free TrialLog in
Avatar of ipjyo
ipjyoFlag for United States of America

asked on

"The process cannot access the file because it is being used by another process" in a windows service?

Hi,
I am getting an error as
"The process cannot access the file because it is being used by another process"
when I was trying to drop files in a folder. My code is as shown below.
The file gets processed most of the times but sometimes I am getting the error message randomly. I am not sure how to track this.
If anybody has any idea of this, Please help me.
Thank you.
Imports System.ServiceProcess
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO
Imports System.ComponentModel
Imports System.Xml
 
Namespace WatchFileSystem
    Public Class FileWatcher
        Inherits ServiceBase
 
        Private components As System.ComponentModel.IContainer = Nothing
        Private fsWatcher As FileSystemWatcher = Nothing
 
        Public Sub New()
            InitializeComponent()
        End Sub
 
        Public Shared Sub Main()
            Dim ServicesToRun As System.ServiceProcess.ServiceBase()
            ServicesToRun = New System.ServiceProcess.ServiceBase() {New FileWatcher()}
            System.ServiceProcess.ServiceBase.Run(ServicesToRun)
        End Sub
 
        Private Sub InitializeComponent()
            Me.ServiceName = "Test Service"
        End Sub
 
        Private Sub IntializeFileSystemWatcher()
            'Create File System Watcher for DAT files
            fsWatcher = New System.IO.FileSystemWatcher("C:\Sample Files", "*.DAT")
            ' Add event handlers for new files.
            AddHandler fsWatcher.Created, AddressOf OnFileCreated
            ' Begin watching.
            fsWatcher.EnableRaisingEvents = True
        End Sub
 
        Private Sub OnFileCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
            Dim ConvertXml As Translator
            ConvertXml = New Translator()
            Dim XmlDoc As New XmlDocument
            Try
                XmlDoc = ConvertXml.Prepare(e.FullPath.ToString())
                XmlDoc.Save("c:\\dynamicsample.xml")
                System.Windows.Forms.MessageBox.Show(e.FullPath.ToString(), "Success", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information, System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.ServiceNotification)
                File.Delete(e.FullPath.ToString())
            Catch ex As Exception
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString(), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information, System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.ServiceNotification)
            End Try
        End Sub
 
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If components IsNot Nothing Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
 
        Protected Overrides Sub OnStart(ByVal args() As String)
            IntializeFileSystemWatcher()
        End Sub
 
        'Override the OnStop method
        Protected Overrides Sub OnStop()
            'Add code to clean up or stop some things
 
        End Sub
    End Class
End Namespace

Open in new window

Avatar of NotLogical
NotLogical
Flag of Canada image

Hi ipjyo,

Is this related to processing the queue every 60 seconds as well - the code looks very similar? I have a complete solution for you, if you are interested...

Thanks,

NotLogical
Avatar of ipjyo

ASKER

Hi @NotLogical,
Actually I have not tried the queue part yet. I am getting the above error sometimes when I try to place a file in that folder "C:\Sample Files". Sometimes it is working. It looks weird to me.
But if you have the code that is very helpful for me. I would appreciate if you can post the code.
Thank you
ASKER CERTIFIED SOLUTION
Avatar of NotLogical
NotLogical
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipjyo

ASKER

Hi NotLogical,
Thank you very much for your help and suggestions.
It is good to have this
XmlDoc.Save(strQueueItem.Replace(".dat", ".xml"))
This is what I actually wanted.
And please give me sometime to implement the above code. I will get back to you.
Thanks again!

Avatar of ipjyo

ASKER

Hi @NotLogical,
Your code is working exactly how I wanted.
Thanks very much
You're very welcome! Glad I could assist!