简体   繁体   中英

FileSystemWatcher XML VB.Net

I'm writing a program for a radio station that scans a XML file which the now playing song is updated to, I then need the program to send the pulled artist name and song name from the XML to a URL so that I can display the data on the web.

Here is the code that I have written to scan and display the data that I need from the XML:

Imports System.IO
Imports System.Xml
Public Class Form1

    Private Sub BrowseButton_Click(sender As Object, e As EventArgs) Handles BrowseButton.Click

        If OpenFileDialog1.ShowDialog = DialogResult.OK Then

            XMLFileDirectoryTextBox.Text = OpenFileDialog1.FileName

        End If

    End Sub

    Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click

        If (XMLFileDirectoryTextBox.Text = "") Then

            MessageBox.Show("XML file not found. Please select your XML file and try again.")
        Else

            If (System.IO.File.Exists(XMLFileDirectoryTextBox.Text.ToString())) Then

                Dim document As XmlReader = New XmlTextReader(XMLFileDirectoryTextBox.Text.ToString())

                While (document.Read())

                    Dim type = document.NodeType

                    If (type = XmlNodeType.Element) Then

                        If (document.Name = "ARTIST") Then

                            ArtistNameTextBox.Visible = True
                            ArtistNameTextBox.Text = document.ReadInnerXml.ToString()

                        End If

                        If (document.Name = "TITLE") Then

                            SongTitleTextBox.Visible = True
                            SongTitleTextBox.Text = document.ReadInnerXml.ToString()

                        End If

                    End If

                End While

            End If

        End If

    End Sub

Because the XML is constantly changing I need the file to be constantly watched for changes and because of this I decided to use the SystemFileWatcher class in VB. I've tried to give it a go but from what I can see SystemFileWatcher only works with watching a folder for changes and not a specific file.

I'm quite new to coding so sorry if theirs any bad techniques within the code above (I'm still learning), if anyone would kindly help me out it would be much appreciated.

Thanks.

Yes you can watch for a file. There is a good example on MSDN that should get you started. Here is a slight adaptation of that which watches a single file:

Public Sub CreateFileWatcher(ByVal path As String, ByVal filename as String)
    'Create a new FileSystemWatcher and set its properties.
    Dim watcher As New FileSystemWatcher()
    watcher.Path = path
    'Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. 
    watcher.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
    'Only watch the file we are interested in
    watcher.Filter = filename

    'Add event handlers.
    AddHandler watcher.Changed, AddressOf OnChanged
    AddHandler watcher.Created, AddressOf OnChanged
    AddHandler watcher.Deleted, AddressOf OnChanged
    AddHandler watcher.Renamed, AddressOf OnRenamed

    ' Begin watching.
    watcher.EnableRaisingEvents = True
End Sub

' Define the event handlers.
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
    ' Specify what is done when a file is changed, created, or deleted.
   Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)
End Sub

Private Shared Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
    ' Specify what is done when a file is renamed.
    Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM