Solved

File Changed Event in VB.NET

Posted on 2004-10-21
14
268 Views
Last Modified: 2010-04-23
Refering to my earlier post: http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21165036.html, I am able to log all the files activities. However certain files I do not want to log the temp file generated by the application (for instance, when I execute test.xls, 3 temp files have been created (these temp files not necessary end with .tmp or begin with ~) and these 4 files are recorded). I just want to record test.xls has been changed.

Please advice.

TIA.
0
Comment
Question by:bpyeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
14 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 12367912
have a look at FileSystemWatcher ... look for the Changed Event
0
 
LVL 19

Expert Comment

by:Desp
ID: 12367918
Heres sample code...just copy/paste/run:

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public watchfolder As System.IO.FileSystemWatcher

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtFolder As System.Windows.Forms.TextBox
    Friend WithEvents txtFolderactivity As System.Windows.Forms.TextBox
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents btnStop As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtFolder = New System.Windows.Forms.TextBox()
        Me.txtFolderactivity = New System.Windows.Forms.TextBox()
        Me.btnStart = New System.Windows.Forms.Button()
        Me.btnStop = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'txtFolder
        '
        Me.txtFolder.Location = New System.Drawing.Point(56, 8)
        Me.txtFolder.Name = "txtFolder"
        Me.txtFolder.Size = New System.Drawing.Size(440, 20)
        Me.txtFolder.TabIndex = 0
        Me.txtFolder.Text = "C:\"
        '
        'txtFolderactivity
        '
        Me.txtFolderactivity.Location = New System.Drawing.Point(56, 32)
        Me.txtFolderactivity.Multiline = True
        Me.txtFolderactivity.Name = "txtFolderactivity"
        Me.txtFolderactivity.Size = New System.Drawing.Size(440, 120)
        Me.txtFolderactivity.TabIndex = 1
        Me.txtFolderactivity.Text = ""
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(504, 8)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(64, 24)
        Me.btnStart.TabIndex = 2
        Me.btnStart.Text = "Start"
        '
        'btnStop
        '
        Me.btnStop.Location = New System.Drawing.Point(504, 128)
        Me.btnStop.Name = "btnStop"
        Me.btnStop.Size = New System.Drawing.Size(64, 24)
        Me.btnStop.TabIndex = 3
        Me.btnStop.Text = "Stop"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 24)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Folder"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(48, 24)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Activity"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(576, 197)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtFolderactivity, Me.txtFolder, Me.Label2, Me.Label1, Me.btnStop, Me.btnStart})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        If (txtFolder.Text.Length > 0 And Directory.Exists(txtFolder.Text)) Then

            watchfolder = New System.IO.FileSystemWatcher()

            'Add a list of Filter we want to specify
            'make sure you use OR for each Filter as we need to
            'all of those

            'this is the path we want to monitor
            watchfolder.Path = txtFolder.Text

            'Add a list of Filter we want to specify
            'make sure you use OR for each Filter as we need to
            'all of those

            watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
            watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
            watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes

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

            ' add the rename handler as the signature is different
            AddHandler watchfolder.Renamed, AddressOf logrename

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

            btnStart.Enabled = False
            btnStop.Enabled = True
        End If
    End Sub

    Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
            txtFolderactivity.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Created Then
            txtFolderactivity.Text &= "File " & e.FullPath & " has been created" & vbCrLf
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
            txtFolderactivity.Text &= "File " & e.FullPath & " has been deleted" & vbCrLf
        End If

    End Sub

    'This is the code for handling the Renamed event raised by the FileSystemWatcher class.
    Public Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
        txtFolderactivity.Text &= "File" & e.OldName & " has been renamed to " & e.Name & vbCrLf
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        ' Stop watching the folder
        watchfolder.EnableRaisingEvents = False
        btnStart.Enabled = True
        btnStop.Enabled = False
    End Sub

End Class
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12367982
Hi bpyeo,

As far as I know temporary files created by office programs have either a .tmp extension or none

so maybe you can try this

    Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
        ' Specify what is done when a file is changed, created, or deleted.
        If e.FullPath.ToUpper.IndexOf("FILEWATCHERFILES") > -1 Or _
             e.FullPath.ToUpper.IndexOf("*.") = -1 Or _
             e.FullPath.ToUpper.IndexOf(".TMP") > -1 Then
            Exit Sub
        End If
        Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType.ToString)
    End Sub


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bpyeo
ID: 12368003
Thanks for your rapid response. However it did not solve my problem. I have obtained the following output using your program:

File C:\temp\daccf100 has been created
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\controls on sheet.xls has been modified
File C:\temp\controls on sheet.xls has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\daccf100 has been modified
File C:\temp\controls on sheet.xls~rf1fccdd1.tmp has been created
File C:\temp\controls on sheet.xls has been deleted
File C:\temp\controls on sheet.xls~rf1fccdd1.tmp has been modified
Filedaccf100 has been renamed to controls on sheet.xls
File C:\temp\controls on sheet.xls~rf1fccdd1.tmp has been modified
File C:\temp\controls on sheet.xls has been modified
File C:\temp\controls on sheet.xls~rf1fccdd1.tmp has been deleted

What I did was open an Excel file "controls on sheet.xls", enable the macro and then save the file. I just want to have a line that said "controls on sheet.xls" has been modified.

Please advice.

TIA.
0
 

Author Comment

by:bpyeo
ID: 12368167
Hi RonaldBiemans, I have tried your suggestion in the logchange procedure and I have no event notification at all. As I did not implement your suggestion in the logrename procedure and I have the following output:

Fileb5a91200 has been renamed to controls on sheet.xls

Please advice.

TIA.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12368485
Hi bpyeo,

I'm just trying here but could you test this

 Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

        Dim myfile As New FileInfo(e.FullPath)
        If myfile.Attributes = -1 Then
            Exit Sub
        End If

        Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType.ToString)
    End Sub
0
 

Author Comment

by:bpyeo
ID: 12377516
Hi RonaldBiemans, I have tried your suggestion in the logchange procedure, remarking the first suggestion and inserted the code for the second suggestion, and I have the following output:

File C:\temp\0d98b000 has been created
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\controls on sheet.xls has been modified
File C:\temp\controls on sheet.xls has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\0d98b000 has been modified
File C:\temp\controls on sheet.xls~rfb8a0e0.tmp has been created
File C:\temp\controls on sheet.xls~rfb8a0e0.tmp has been modified
File 0d98b000 has been renamed to controls on sheet.xls
File C:\temp\controls on sheet.xls has been modified

If I have added the coding from both your first and second suggestion, I have the same output as before.

Please advice.

TIA.
0
 
LVL 19

Expert Comment

by:Desp
ID: 12377957
Change the logchange function to respond to *Changed* event of *controls on sheet.xls*:

    Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
            If e.Name = "controls on sheet.xls" Then
                txtFolderactivity.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
            End If
        End If
    End Sub
0
 
LVL 19

Expert Comment

by:Desp
ID: 12377964
That is the response of your first reply to my comment. just checked mails..
0
 

Author Comment

by:bpyeo
ID: 12378189
Hi Desp, thanks for your suggestion, I could not hardcode the Excel file as I am watching over a folder that may contain multiple office documents, programming files, etc and I just do not want to record any temp files that are being generated by the application.

TIA.
0
 
LVL 19

Accepted Solution

by:
Desp earned 500 total points
ID: 12378220
   Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
            If Path.GetExtension(e.FullPath) <> "" Or Path.GetExtension(e.FullPath) <> ".tmp" Then
                txtFolderactivity.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
            End If
        End If
    End Sub
0
 

Author Comment

by:bpyeo
ID: 12378628
Hi Desp, it works (but with a slight problem) and the following is the output:

File c:\temp\controls on sheet.xls has been modified
File c:\temp\controls on sheet.xls has been modified
File c:\temp\controls on sheet.xls has been deleted
File c:\temp\controls on sheet.xls has been modified

I have traced through the entire process and realized that this file "controls on sheet.xls" has been deleted by the Excel application and a temp file "daccf100" has been created, modified and finally renamed to "controls on sheet.xls". Now I do not see the temp file appearing. How could I tell whether the file is being deleted by an application (do not want to reflect in the file activity) or the file being deleted by the user (want to reflect in the file activity)?

Please advice.

TIA.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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