Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Watcher Exception Handling/Avoiding!?

Status
Not open for further replies.

Japskunk72

Programmer
Feb 19, 2003
64
US
I found some code to watch files on my hard drive for changes. I want to be able to save files and have them auto organize if they are not in the right place. i can handle that code, but i am having file accesssing rights issues. for instance I have been testing the program by making a new Document from Microsoft Word. when i go to save as: and tell it to go in the specified directory that the file watch is watching, it doesnt detect any new files created. it will give be an error through my try and catch. the error is:

System.IO.IOException: The process cannot access the file "c:\documents and settings\username\desktop\SomeDoc.doc" because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at FileWatcherMain.Form1.objFSW_Created(Object sender, FileSystemEventArgs e)

My code is this :

Imports System.IO
Imports System.IO.IOException
Public Class Form1
Inherits System.Windows.Forms.Form
Protected WithEvents objFSW As New FileSystemWatcher()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
objFSW.Path = "c:\"
objFSW.EnableRaisingEvents = True
objFSW.Filter = "*.*"
objFSW.IncludeSubdirectories = True
End Sub
Private Sub objFSW_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles objFSW.Created
Try
Dim objStream As New FileStream(e.FullPath, FileMode.OpenOrCreate)
Dim objRead As New StreamReader(objStream)
LSTNew.Items.Add(e.FullPath)
objStream.Close()
Catch Errr As System.IO.IOException
Console.WriteLine(Errr)
End Try
End Sub
End Class

all i have on the form is a lstbox to visualy update named LSTNew

Is there a way to read the file attributes without having to open the file and cause it to be used? I am new to VB.Net and had problems with ReaddirectoryChangesW in VB6.0 thats why i am trying to take this new route. any info would be very helpful
Thanks in advanced
 
Try everything in the system.io class like directory and file It will let you read the attributes of a file , but you won't be able to move or change them untill they are released by word.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top