locked
Is there a way to find if the source file in a vb.net project is changed since last build RRS feed

  • Question

  • I want this filesystemwatcher.changed event to be fired when I build the application instead when I save the file. Can any one help me finding the solution. My intention is to raise the filesystemwatcher.changed event when there is a change in the file from last build. Please let me know if this possible.

    Thanks in Advance!!

    Regards,

    Rahul

    Thursday, September 13, 2012 7:02 PM

Answers

  • Hey Rahul.

    Actually when you build the source code, changes occur in two places: in your source code file, and in the generated assembly file (.exe, .dll, etc).  So in your case, if you want to be notified when you change the source code file (.vb, .cs, etc), then use the FileSystemWatcher to watch the source code file like this:

      Sub Main()
            Dim watcher As New FileSystemWatcher("[Source code path]")
            watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.Size
            watcher.EnableRaisingEvents = True
            AddHandler watcher.Changed, AddressOf watcher_Changed
        End Sub
    
        Sub watcher_Changed(sender As Object, e As FileSystemEventArgs)
            'Do what ever you want here
        End Sub


    And if you want to be notified only when you build the application, use the Watcher like this:

      Sub Main()
            Dim watcher As New FileSystemWatcher("[assembly file path]")
            watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.Size
            watcher.EnableRaisingEvents = True
            AddHandler watcher.Changed, AddressOf watcher_Changed
        End Sub
    
        Sub watcher_Changed(sender As Object, e As FileSystemEventArgs)
            'Do what ever you want here
        End Sub

    Regards,

    Abdul

    • Proposed as answer by Abdul A. Ali Friday, September 14, 2012 7:42 PM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:50 PM
    Friday, September 14, 2012 4:22 AM
  • Hey Rahul.


    Starting with your first requirement, I recommend you watch the source code files for change, not the assembly that gets generated after you build the application. The reason is because, every time you build you application, all the changes will be saved automatically to the source code files. So you can just sit back and relax, watch the source code files for change, and there you go..

    Here's the scenario:

    1- Create a FileSystemWatcher and add a handler for the FileSystemWatcher.Changed event.

    2- In the event handler, add your custom code (the code that runs the macro's and everything).

    3- Make some changes in your source code, and then rebuild the application.

    4- As soon as the build completes, the changes will reflect in both the source code file and the generated assemblies.

    5- Because there's a change, the FileSystemWatcher.Changed event will get fired, and your handler gets executed.


    And regarding you other requirement, which is how to use the events generically, the answer is simple. To get your working directory (the directory at which your application executes), use either the Environment.CurrentDirectory property or the AppDomain.CurrentDomain.SetupInformation.ApplicationName property. Then after you retrieve the directory you can set the FileSystemWatcher.Path to that path and the FileSystemWatcher.Filter to '.vb, .cs, etc'.


    I hope this answer helps.


    Regards,

    Abdul


    • Proposed as answer by Mike Feng Tuesday, September 18, 2012 11:24 AM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:50 PM
    Friday, September 14, 2012 8:33 PM

All replies

  • I want this filesystemwatcher.changed event to be fired when I build the application instead when I save the file. Can any one help me finding the solution. My intention is to raise the filesystemwatcher.changed event when there is a change in the file from last build. Please let me know if this possible.

    Thanks in Advance!!

    Regards,

    Rahul

    I want to extract all the .vb class file names which got changed from the last build when I build the changed project. Please give me some idea in doing this.

    Thanks,

    Rahul

    Thursday, September 13, 2012 8:14 PM
  • Hey Rahul.

    Actually when you build the source code, changes occur in two places: in your source code file, and in the generated assembly file (.exe, .dll, etc).  So in your case, if you want to be notified when you change the source code file (.vb, .cs, etc), then use the FileSystemWatcher to watch the source code file like this:

      Sub Main()
            Dim watcher As New FileSystemWatcher("[Source code path]")
            watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.Size
            watcher.EnableRaisingEvents = True
            AddHandler watcher.Changed, AddressOf watcher_Changed
        End Sub
    
        Sub watcher_Changed(sender As Object, e As FileSystemEventArgs)
            'Do what ever you want here
        End Sub


    And if you want to be notified only when you build the application, use the Watcher like this:

      Sub Main()
            Dim watcher As New FileSystemWatcher("[assembly file path]")
            watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.Size
            watcher.EnableRaisingEvents = True
            AddHandler watcher.Changed, AddressOf watcher_Changed
        End Sub
    
        Sub watcher_Changed(sender As Object, e As FileSystemEventArgs)
            'Do what ever you want here
        End Sub

    Regards,

    Abdul

    • Proposed as answer by Abdul A. Ali Friday, September 14, 2012 7:42 PM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:50 PM
    Friday, September 14, 2012 4:22 AM
  • Hi Abdul,

    Thank you very much for the quick response. I have few questions!

    1) Can I put this code in the build event on build begin like below:

      Public Sub BuildEvents_OnBuildBegin() Handles BuildEvents.OnBuildBegin
    
            'MsgBox("Build Started")
            'MsgBox(DTE.ActiveDocument.ProjectItem.Document.Path)
            Dim watcher As New FileSystemWatcher(Assembly.GetExecutingAssembly().Location)
            watcher.Filter = "*.vb"
            watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.Size
            AddHandler watcher.Changed, AddressOf logchange
    
            watcher.EnableRaisingEvents = True
        End Sub
        Private Sub logchange(ByVal source As Object, ByVal e As  _
                            System.IO.FileSystemEventArgs)
            If e.ChangeType = IO.WatcherChangeTypes.Changed Then
                MsgBox("File " & e.FullPath & _
                                 " has been modified")
            End If
        End Sub

    2) I want to run this event generically. How can I get the generic Assembly path?

    Friday, September 14, 2012 6:59 PM
  • Hey Rahul.

    Regarding your first question, I honestly have never used the BuildEvents.OnBuildBegin event but I think you can do that. But why are you registering the FileSystemWatcher.Changed event within the OnBuildBegin. If you do this, the event gets registered on every build over and over again.

    You can just register the event in somewhere else like when the form is loading. Then as soon as the source code gets modified or the assembly gets regenerated, the FileSystemWatcher.Changed event will fire and your handler will get executed.

    And regarding you second question, I don't really understand what you mean by "running the event generically". Could you elaborate more on that?

    Regards,

    Abdul




    • Edited by Abdul A. Ali Thursday, September 20, 2012 6:56 PM Grammatical error
    Friday, September 14, 2012 7:40 PM
  • Hi Abdul,

    I will first elaborate my requirement. In my project I have many class libraries available to make the life easy and comfortable I have created an automatic documenting of a source code using macro's in VS2010. So for every change in the code the user should run the respective macro and provide the data in the automatic generated comment section for the function or property. Some times the user may forget to run the macro. So in this scenario I want to prompt the user to comment the code for the new changes. So I thought of doing this when the user builds the application. i.e. when the user build's the application I want to check if the source god has be changed from the last build. If there is a change then I want to check if he had commented the changes.

    About running the even generically: there are many applications available in my project. So when I do this task I don't want to hard code the path of the current application assembly path. Is there any way that I can provide the Assembly path regardless of which application I run?

    Thanks,

    Rahul


    • Edited by Rahulrohit Friday, September 14, 2012 8:10 PM
    Friday, September 14, 2012 8:09 PM
  • Hey Rahul.


    Starting with your first requirement, I recommend you watch the source code files for change, not the assembly that gets generated after you build the application. The reason is because, every time you build you application, all the changes will be saved automatically to the source code files. So you can just sit back and relax, watch the source code files for change, and there you go..

    Here's the scenario:

    1- Create a FileSystemWatcher and add a handler for the FileSystemWatcher.Changed event.

    2- In the event handler, add your custom code (the code that runs the macro's and everything).

    3- Make some changes in your source code, and then rebuild the application.

    4- As soon as the build completes, the changes will reflect in both the source code file and the generated assemblies.

    5- Because there's a change, the FileSystemWatcher.Changed event will get fired, and your handler gets executed.


    And regarding you other requirement, which is how to use the events generically, the answer is simple. To get your working directory (the directory at which your application executes), use either the Environment.CurrentDirectory property or the AppDomain.CurrentDomain.SetupInformation.ApplicationName property. Then after you retrieve the directory you can set the FileSystemWatcher.Path to that path and the FileSystemWatcher.Filter to '.vb, .cs, etc'.


    I hope this answer helps.


    Regards,

    Abdul


    • Proposed as answer by Mike Feng Tuesday, September 18, 2012 11:24 AM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:50 PM
    Friday, September 14, 2012 8:33 PM