Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File.copyTo problems

Status
Not open for further replies.

majordog

Programmer
Jul 8, 2002
222
0
0
CA

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
 
Hi Majordog,

I've just solve a similar problem, I think.

First of all, identify what process use your object.
Then point it to nothing at the end of your code
(ex : currentImage=nothing)

At the end, use a Garbage Collector :

imports system.gc
<

Code>

yourobject_that_is_from_your_filestream = nothing
gc.collect()

But, in my case, this was not enough : in fact, when I close my window that use the filestream object (eg:currentImage), I must use the Garbage collector again (same syntax : me.yourobject_that_is_from_your_filestream = nothing
gc.collect() )

Hope this help you...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top