2016-07-21 4 views
1

지금 재생중인 노래가 업데이트되는 XML 파일을 스캔하는 라디오 방송국을위한 프로그램을 작성 중이므로 XML에서 끌어온 아티스트 이름과 노래 이름을 보내도록 프로그램이 필요합니다. URL을 사용하여 웹에 데이터를 표시 할 수 있습니다. 는 XML 끊임없이 내가 끊임없이 변화를 감시 할 파일을 필요로 변경되기 때문에FileSystemWatcher XML VB.Net

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가 볼 수있는 것에서 만 특정 파일이 아닌 변경 사항을보기 위해 폴더를 볼 수 있습니다.

코드 작성에 익숙하지 않으므로 (위의 코드에서 잘못된 기술을 배우면) 미안하지만 누군가 나를 친절하게 도와 주시면 대단히 감사하겠습니다.

감사합니다.

답변

0

예, 파일을 볼 수 있습니다. 시작해야 할 good example on 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 
+0

#의 대답은 [FileSystemWatcher.NotifyFilter 재산권 (그냥 붙여 넣기는 C 그 https://msdn.microsoft.com/en-us/library/ : 여기에 하나의 파일을 시계 그 약간의 적응이다 system.io.filesystemwatcher.notifyfilter (v = vs.85) .aspx/html? cs-save-lang = 1 & cs-lang = csharp # code-snippet-5)이 파일에 링크되어 있지만 붙여 넣기라고 말합니다 – Plutonix

+0

@Matt Wilko이 문제를 보았지만 문제는 프로그램에서 사용하려는 정확한 XML을 정의 할 수있는 openfiledialog가 있고, 위의 코드는 폴더를 선택한 다음 (* .txt) 변경 사항을 확인합니다. 파일하지만 내 경우에는 XML이있는 폴더가 필요하다. –

+0

그래서 단지 하나의 파일을보아야하는'watcher.Filter = "MyExactFilename.xml"을 변경하십시오 –