Solved

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

Posted on 2004-08-30
12
674 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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