Solved

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

Posted on 2004-08-30
12
679 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

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!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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