Hey All,
Working with VB.net, writing a windows service that monitors the folder activity on a share drive. When a text file is dropped into the folder, the file is copied and moved to a private directory on the local drive. I am having problems with copying the file over to the local drive. I catch this error when I try to copy the file:
The error is: 'System.IO.IOException: The process cannot access the file "C:\NonCnformance\'filename.txt' " because it is being used by another process at System.IO_Error.WINIOError...etc
I am including the code I use in hopes someone can explain where I have gone wrong..:
Private WithEvents NonConformance As FileSystemWatcher
Private oFile As System.IO.File
Private oCopiedFile As System.IO.File
Private oRead As System.IO.StreamReader
Private myFilestream As FileStream
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
If Not EventLog.SourceExists("ConformanceSource") Then
EventLog.CreateEventSource("ConformanceSource", "NonConformanceLog")
End If
EventLog1.Source = "ConformanceSource"
EventLog1.Log = "NonConformanceLog"
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents EventLog1 As System.Diagnostics.EventLog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader
Me.EventLog1 = New System.Diagnostics.EventLog
CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).BeginInit()
'
'EventLog1
'
Me.EventLog1.Log = "NonConformanceLog"
' Me.EventLog1.Source = CType(configurationAppSettings.GetValue("NonConformance", GetType(System.String)), String)
'
'NonConformance
'
Me.ServiceName = "NonConformance"
CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).EndInit()
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New NonConformance}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
#End Region
Private oFileInfo As System.IO.FileInfo
Dim oFileStream As System.IO.FileStream
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.
If Not EventLog.SourceExists("ConformanceSource") Then
EventLog1.CreateEventSource("ConformanceSource", "NonConformance")
End If
NonConformance = New System.IO.FileSystemWatcher
'this is the path to the folder to be monitored
NonConformance.Path = "S:\ NonConformances\NonConformance Copies\"
EventLog1.WriteEntry("drive has been mapped")
'List of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
NonConformance.NotifyFilter = IO.NotifyFilters.DirectoryName
NonConformance.NotifyFilter = NonConformance.NotifyFilter Or _
IO.NotifyFilters.FileName
NonConformance.NotifyFilter = NonConformance.NotifyFilter Or _
IO.NotifyFilters.Attributes
'Set this property to true to start watching
NonConformance.EnableRaisingEvents = True
' Add the handler to each event - Only reacting to new files at creation
AddHandler NonConformance.Created, AddressOf logchange
'AddHandler NonConformance.Changed, AddressOf logchange
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)
'If file has been changed....
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
Exit Sub
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
EventLog1.WriteEntry("Created")
Try
oFileInfo = New FileInfo(filename:=e.FullPath)
EventLog1.WriteEntry("'" & e.FullPath & "' ")
oFileStream = New FileStream(e.FullPath, FileMode.Open)
'oFileStream = oFileInfo.Create
oFileInfo.CopyTo(destFilename:="C:\\NonConformance\ '" & e.Name & "' ")
EventLog1.WriteEntry("C:\\NonConformance\ '" & e.Name & "' ")
Catch vf As Exception
EventLog1.WriteEntry("The error is: '" & vf.ToString & "' ")
Finally
oFileStream.Close()
EventLog1.WriteEntry("Completed. Check directory for file.")
End Try
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
Exit Sub
End If
End Sub
Private Sub NonConformance_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles NonConformance.Created
EventLog1.WriteEntry("Inside Nonconformance created just started")
logchange(sender, e)
EventLog1.WriteEntry("Inside Nonconformance created just finished")
End Sub