简体   繁体   English

FileSystemWatcher XML VB.Net

[英]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. 我正在为一个广播电台编写程序,该程序扫描一个XML文件,将正在播放的歌曲更新为该文件,然后我需要该程序将提取的歌手姓名和歌曲名称从XML发送到URL,以便显示网络上的数据。

Here is the code that I have written to scan and display the data that I need from the XML: 这是我编写的用于扫描和显示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. 因为XML在不断变化,所以我需要不断监视文件的变化,因此,我决定在VB中使用SystemFileWatcher类。 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. 我已经尝试过了,但是从我看到的结果来看,SystemFileWatcher仅适用于监视文件夹中的更改而不是特定文件。

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. 在MSDN上有一个很好的例子可以帮助您入门。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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