filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
Go Back  Xtreme Visual Basic Talk > > > filesystemwatcher - check if file is open or used by another process!


Reply
 
Thread Tools Display Modes
  #1  
Old 08-13-2009, 09:09 AM
putte putte is offline
Newcomer
 
Join Date: Aug 2009
Posts: 1
Default filesystemwatcher - check if file is open or used by another process!


I have a filesystemwatcher that watches a certain folder.. Now i use system.thread.sleep to be sure that the file is completely copied to the folder that i watch..But this is not a good way!

I know there is a way to check if the file is open or used by another process, but i dont know the code..

Can someone please help me with this?

Thank you!

Here is my code if it helps!

______________________________________________

Imports MySql.Data.MySqlClient
Imports System
Imports System.IO
Imports System.Xml
Imports System.Net
Imports System.Text
Imports Utilities.FTP
Imports System.Web.HttpUtility

Public Class Form1
Public watchfolder As FileSystemWatcher
Public movetofolder As String
Public ip As String
Public user As String
Public pass As String
Public completed_folder As String
Public error_folder As String
Public filepattern As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

btn_stop.Enabled = False
btn_start.Enabled = True

End Sub

Public Sub btn_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_start.Click

watchfolder = New System.IO.FileSystemWatcher()

Dim m_xmld As XmlDocument
Dim m_node As XmlNode
Dim m_nodelist As XmlNodeList

m_xmld = New XmlDocument()

m_xmld.Load("c:\settings.xml")
m_nodelist = m_xmld.SelectNodes("/settings")

For Each m_node In m_nodelist
watchfolder.Path = m_node.ChildNodes.Item(0).InnerText
ip = m_node.ChildNodes.Item(1).InnerText
user = m_node.ChildNodes.Item(2).InnerText
pass = m_node.ChildNodes.Item(3).InnerText
completed_folder = m_node.ChildNodes.Item(4).InnerText
error_folder = m_node.ChildNodes.Item(5).InnerText
filepattern = m_node.ChildNodes.Item(6).InnerText
Next

'Add a list of Filter we want to specify
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or NotifyFilters.LastAccess
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or NotifyFilters.LastWrite

'watchfolder.Filter = "*.pdf"

' add the handler to each event
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange

'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True

btn_start.Enabled = False
btn_stop.Enabled = True

log("Servicen startade!")
End Sub

Public Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
Dim pattern As String
If e.ChangeType = IO.WatcherChangeTypes.Created Then
System.Threading.Thread.Sleep(10000)
If e.Name.EndsWith(".pdf") Then
If e.Name.StartsWith(filepattern) Then
Try
Dim myFtp As New FTPclient(ip, user, pass)
myFtp.Upload(e.FullPath)
My.Computer.FileSystem.MoveFile(e.FullPath, completed_folder & e.Name, True)
log(e.Name & " FTP:ades till " & ip & "!")
Catch ex As Exception
My.Computer.FileSystem.MoveFile(e.FullPath, error_folder & e.Name, True)
log(ex.Message)
End Try
Else
My.Computer.FileSystem.MoveFile(e.FullPath, error_folder & e.Name, True)
log("FAIL!! Wrong filetype! PDF mover VBL har flyttat följande fil: " & e.Name & " till error mappen!!")
End If
Else
My.Computer.FileSystem.MoveFile(e.FullPath, error_folder & e.Name, True)
log("FAIL!! Wrong filetype! PDF mover VBL har flyttat följande fil: " & e.Name & " till error mappen!!")
End If
End If

End Sub

Private Sub btn_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_close.Click
Me.Close()
End Sub

Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click

Try
watchfolder.EnableRaisingEvents = False
btn_stop.Enabled = False
btn_start.Enabled = True
log("Servicen stoppades!")
Catch ex As Exception
log(ex.Message)
End Try

End Sub

Public Sub log(ByVal message As String)
Try
My.Computer.FileSystem.WriteAllText("C:\Documents and Settings\Putte\My Documents\Visual Studio 2008\Projects\LogFile\LogFile\log.txt", Now().ToString & " - " & message & vbCrLf, True)
Catch ex As Exception
End Try
End Sub

End Class
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process! filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
 
filesystemwatcher - check if file is open or used by another process!
filesystemwatcher - check if file is open or used by another process!
 
-->