繁体   English   中英

FileSystemWatcher XML VB.Net

[英]FileSystemWatcher XML VB.Net

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

这是我编写的用于扫描和显示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

因为XML在不断变化,所以我需要不断监视文件的变化,因此,我决定在VB中使用SystemFileWatcher类。 我已经尝试过了,但是从我看到的结果来看,SystemFileWatcher仅适用于监视文件夹中的更改而不是特定文件。

我对编码非常陌生,如果他们在上面的代码中有任何不好的技术(我还在学习),对不起,如果有人可以帮助我,将不胜感激。

谢谢。

是的,您可以查看文件。 在MSDN上有一个很好的例子可以帮助您入门。 这是监视单个文件的内容的略微改编:

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