Solved

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

Posted on 2004-08-30
12
686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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