簡體   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