Solved

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

Posted on 2004-08-30
12
668 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

18 Experts available now in Live!

Get 1:1 Help Now