locked
filesystemwatcher - check if file is open or used by another process! RRS feed

  • Question

  • User-1184346870 posted

    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!

    ______________________________________________--

    1. Imports MySql.Data.MySqlClient  
    2. Imports System  
    3. Imports System.IO  
    4. Imports System.Xml  
    5. Imports System.Net  
    6. Imports System.Text  
    7. Imports Utilities.FTP  
    8. Imports System.Web.HttpUtility  
    9. Imports System.Xml.XPath  
    10.   
    11. Public Class Form1  
    12.     Public watchfolder As FileSystemWatcher  
    13.     Public movetofolder As String  
    14.     Public ip As String  
    15.     Public user As String  
    16.     Public pass As String  
    17.     Public completed_folder As String  
    18.     Public error_folder As String  
    19.     Public filepattern As String  
    20.     Public lstFilePatterns As New List(Of [String])()  
    21.   
    22.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
    23.   
    24.         btn_stop.Enabled = False  
    25.         btn_start.Enabled = True  
    26.   
    27.     End Sub  
    28.   
    29.     Public Sub btn_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_start.Click  
    30.   
    31.         watchfolder = New System.IO.FileSystemWatcher()  
    32.   
    33.         Dim m_xmld As XmlDocument  
    34.         Dim m_node As XmlNode  
    35.         Dim m_nodelist As XmlNodeList  
    36.   
    37.         m_xmld = New XmlDocument()  
    38.   
    39.         m_xmld.Load("c:\settings.xml")  
    40.         m_nodelist = m_xmld.SelectNodes("/settings")  
    41.   
    42.         For Each m_node In m_nodelist  
    43.             watchfolder.Path = m_node.ChildNodes.Item(0).InnerText  
    44.             ip = m_node.ChildNodes.Item(1).InnerText  
    45.             user = m_node.ChildNodes.Item(2).InnerText  
    46.             pass = m_node.ChildNodes.Item(3).InnerText  
    47.             completed_folder = m_node.ChildNodes.Item(4).InnerText  
    48.             error_folder = m_node.ChildNodes.Item(5).InnerText  
    49.             filepattern = m_node.ChildNodes.Item(6).InnerText  
    50.         Next  
    51.   
    52.         Dim xdoc As New XPathDocument("c:\settings.xml")  
    53.         Dim nav As XPathNavigator = xdoc.CreateNavigator()  
    54.         Dim iterator As XPathNodeIterator = nav.[Select]("//filepattern/*")  
    55.         While iterator.MoveNext()  
    56.             lstFilePatterns.Add(iterator.Current.Value)  
    57.         End While  
    58.   
    59.         'Add a list of Filter we want to specify  
    60.         watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName  
    61.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName  
    62.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes  
    63.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or NotifyFilters.LastAccess  
    64.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or NotifyFilters.LastWrite  
    65.   
    66.         'watchfolder.Filter = "*.pdf"  
    67.   
    68.         ' add the handler to each event  
    69.         AddHandler watchfolder.Created, AddressOf logchange  
    70.         AddHandler watchfolder.Changed, AddressOf logchange  
    71.         AddHandler watchfolder.Deleted, AddressOf logchange  
    72.   
    73.         'Set this property to true to start watching  
    74.         watchfolder.EnableRaisingEvents = True  
    75.   
    76.         btn_start.Enabled = False  
    77.         btn_stop.Enabled = True  
    78.   
    79.         log("Servicen startade!")  
    80.     End Sub  
    81.   
    82.     Public Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)  
    83.         Dim pattern As String  
    84.         Dim patterns = From p In lstFilePatterns _  
    85.             Where e.Name.Substring(0, p.Length) = p _  
    86.             Select p  
    87.         If e.ChangeType = IO.WatcherChangeTypes.Created Then  
    88.             System.Threading.Thread.Sleep(10000)  
    89.             If e.Name.EndsWith(".pdf") Then  
    90.                   
    91.                 If patterns.Count > 0 Then  
    92.                     Try  
    93.                         Dim myFtp As New FTPclient(ip, user, pass)  
    94.                         myFtp.Upload(e.FullPath)  
    95.                         My.Computer.FileSystem.MoveFile(e.FullPath, completed_folder & e.Name, True)  
    96.                         log(e.Name & " FTP:ades till " & ip & "!")  
    97.                     Catch ex As Exception  
    98.                         My.Computer.FileSystem.MoveFile(e.FullPath, error_folder & e.Name, True)  
    99.                         log(ex.Message)  
    100.                     End Try  
    101.                 Else  
    102.                     My.Computer.FileSystem.MoveFile(e.FullPath, error_folder & e.Name, True)  
    103.                     log("FAIL!! Wrong filetype! PDF mover VBL har flyttat följande fil: " & e.Name & " till error mappen!!")  
    104.                 End If  
    105.             Else  
    106.                 My.Computer.FileSystem.MoveFile(e.FullPath, error_folder & e.Name, True)  
    107.                 log("FAIL!! Wrong filetype! PDF mover VBL har flyttat följande fil: " & e.Name & " till error mappen!!")  
    108.             End If  
    109.         End If  
    110.   
    111.     End Sub  
    112.   
    113.     Private Sub btn_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_close.Click  
    114.         Me.Close()  
    115.     End Sub  
    116.   
    117.     Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click  
    118.   
    119.         Try  
    120.             watchfolder.EnableRaisingEvents = False  
    121.             btn_stop.Enabled = False  
    122.             btn_start.Enabled = True  
    123.             log("Servicen stoppades!")  
    124.         Catch ex As Exception  
    125.             log(ex.Message)  
    126.         End Try  
    127.   
    128.     End Sub  
    129.   
    130.     Public Sub log(ByVal message As String)  
    131.         Try  
    132.             My.Computer.FileSystem.WriteAllText("C:\Documents and Settings\Putte\My Documents\Visual Studio 2008\Projects\LogFile\LogFile\log.txt", Now().ToString & " - " & message & vbCrLf, True)  
    133.         Catch ex As Exception  
    134.         End Try  
    135.     End Sub  
    136.   
    137. End Class 
    Monday, August 10, 2009 2:46 PM

Answers

  • User187056398 posted

    The Visual Basic section is for Visual Basic questions as they pertain to Asp.Net development. 

    I repeat, this is an Asp.Net web site.  That is why the domain name includes Asp.Net.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 11, 2009 9:20 AM

All replies

  • User187056398 posted

    Using a FileSystemWatcher is not really an asp.net subject (and this is an asp.net web site).

    You'll have better luck on a Windows forms website:  http://forums.asp.net/p/1337412/2699239.aspx#2699239

    Monday, August 10, 2009 5:26 PM
  • User-1184346870 posted

    Well i published this thread under "Visual basic.net" headline.. I supposed this headline was for all visual basic questions!

    Tuesday, August 11, 2009 4:06 AM
  • User187056398 posted

    The Visual Basic section is for Visual Basic questions as they pertain to Asp.Net development. 

    I repeat, this is an Asp.Net web site.  That is why the domain name includes Asp.Net.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 11, 2009 9:20 AM