Solved

'System.IO.IOException error when moving a file.

Posted on 2004-08-30
12
671 Views
Last Modified: 2008-02-01
I've written some code that monitors a fileystem for changes (creation of new files). Every time a file is created in the monitorred filesystem, it's automaticly beeïng moved to another location (C:\Dirty).
The first time the TRIGGER function is triggered, everyting goes fine, but when the trigger fires a second time I get the following error:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The proces has no access to teh file C:\dirty\270704-40be8c06.new because it is beeing used by another process.

The code can't execute the File.Move.

To set up the monitoring I used the:

Imports System.IO
Imports System.Diagnostics
Public WatchFolder As New FileSystemWatcher
WatchFolder.EnableRaisingEvents = True

When I only MONITOR the filesystem (say a messagebox to display only the System.IO.FileSystemEventArgs.Fullpath) it works fine.
The code has problems when I put an ACTION into it (say moving a file, opening it for reading, etc...).

Here is the code that gives me the error:
Private Sub Trigger(ByVal source As Object, ByVal objSystemIO As System.IO.FileSystemEventArgs)

        Dim strfilename
        Dim strdestination As String
        Dim btecounter As Byte

        If objSystemIO.ChangeType = WatcherChangeTypes.Created Then
            strfilename = objSystemIO.FullPath
            btecounter = InStrRev(strFileName, "\")
            strdestination = Mid(strFileName, btecounter)
            File.Move(strFileName, "C:\dirty" & strdestination )
        End If
Exit Sub

Any help ?
0
Comment
Question by:xterminator
  • 5
  • 4
  • 3
12 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11930592
another process still has the file open (and locked) most likely the file is still being created at the time that you are trying to move it.
0
 
LVL 1

Author Comment

by:xterminator
ID: 11930641
I figured thatone out...but how can I solve this problem ? And WHY does it work perfectly the first time the code is triggered?
I suppose it has something to do with the file.move that locks up....
0
 
LVL 4

Expert Comment

by:gdexter
ID: 11935534
You could try using the Move Method from an instance variable rather than calling the Shared (Static method)
0
 
LVL 1

Author Comment

by:xterminator
ID: 11935782
En how do I accomplish this in VB.NET ?
I'm still at beginner level with the VB.NET language.
0
 
LVL 4

Accepted Solution

by:
gdexter earned 125 total points
ID: 11936365
   
Private Sub Trigger(ByVal source As Object, ByVal objSystemIO As System.IO.FileSystemEventArgs)

        Dim strfilename
        Dim strdestination As String
        Dim btecounter As Byte

        If objSystemIO.ChangeType = WatcherChangeTypes.Created Then
            Dim fi As New FileInfo(e.FullPath)
           ' strfilename = objSystemIO.FullPath
           ' btecounter = InStrRev(strFileName, "\")
           ' strdestination = Mid(strFileName, btecounter)
            If(fi.exists) then
               fi.MoveTo("C:\dirty\" & fi.Name)
           End If
         End If
Exit Sub


0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11936379
exactly how is that going to help when the file is in use by another process ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:gdexter
ID: 11937550
It won't, I thought perhaps the static method might cause a problem.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11937982
why ?
0
 
LVL 1

Author Comment

by:xterminator
ID: 11939783
Believe it or not, but it worked !
0
 
LVL 1

Author Comment

by:xterminator
ID: 11940033
Well, I've been a bit too enhousiastic... the suggested answer works with the MoveTo, but there are some additional things I need to do with the file...
I need to open it for reading, check if the file has less than 12 lines written in it, if it does, move it to the dirty folder.
The same problem occures... but this time on the "Dim txtFile As New StreamReader(objSystemIO.FullPath)" line.

I've replaced the checking of the 12 lines with a messagebox... just to make sure that part of the code is working. (But it isn't)

Here is the code:
 If objSystemIO.ChangeType = WatcherChangeTypes.Created Then
            Dim finBestand As New FileInfo(objSystemIO.FullPath)

            If (finBestand.Exists) And finBestand.Length < 2048 Then
                Dim txtFile As New StreamReader(objSystemIO.FullPath)
                Dim bteTeller As Byte
                Dim strLijn As String

                bteTeller = 0
                strLijn = txtFile.ReadLine()
                MsgBox(strLijn)
                txtFile.Close()
                txtFile = Nothing
            End If
        End If
0
 
LVL 4

Expert Comment

by:gdexter
ID: 11941469
Check this article out: It shows you howto define the Created event so that you wait for the file creation process to complete.

http://www.codecomments.com/forum290/message241307.html
0
 
LVL 1

Author Comment

by:xterminator
ID: 11942553
I don't think that's the problem... because the MOVETO works just perfectly. When I change it to open the file for streamreading... and I preform exactly the same handlings... it crashes....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now