|
Imports System.IO
Imports System.Environment
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Vom Windows Form Designer generierter Code "
Public Sub New()
MyBase.New()
' Dieser Aufruf ist für den Windows Form-Designer
' erforderlich.
InitializeComponent()
' Initialisierungen nach dem Aufruf InitializeComponent()
' hinzufügen
TextBox1.Text = GetFolderPath(SpecialFolder.Personal)
TextBox2.Text = "*.*"
AddHandler FileSystemWatcher1.Changed, AddressOf OnChanged
AddHandler FileSystemWatcher1.Created, AddressOf OnChanged
AddHandler FileSystemWatcher1.Deleted, AddressOf OnChanged
AddHandler FileSystemWatcher1.Renamed, AddressOf OnRenamed
StatusBar1.Text = ""
Button4.Visible = False
CheckBox1.Checked = True
End Sub
'...
#End Region
Private sLogFile As String = _
Application.StartupPath & "\logFile.log"
Sub OnChanged(ByVal source As Object, ByVal e As _
FileSystemEventArgs)
ListBox1.Items.Add(e.ChangeType.ToString & ": " & _
e.FullPath & " " & DateTime.Now.ToString)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
End Sub
Sub OnRenamed(ByVal source As Object, ByVal e As _
RenamedEventArgs)
ListBox1.Items.Add(e.ChangeType.ToString & ": " & _
e.OldFullPath & vbNewLine & _
" neuer Name: --> " & e.FullPath & " " & _
DateTime.Now.ToString)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
End Sub
Private Sub StartWatching()
FileSystemWatcher1.Path = TextBox1.Text
FileSystemWatcher1.NotifyFilter = _
(NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or _
NotifyFilters.FileName Or NotifyFilters.DirectoryName)
Dim pattern As String = TextBox2.Text
If pattern = "*" Or pattern = "*.*" Then
pattern = ""
End If
FileSystemWatcher1.Filter = pattern
FileSystemWatcher1.EnableRaisingEvents = True
ListBox1.Items.Clear()
ListBox1.Items.Add(DateTime.Now.ToString)
ListBox1.Items.Add("Überwachen von " & TextBox1.Text & _
" beginnt...")
ListBox1.Items.Add("-----------------------------------------")
End Sub
Private Sub StopWatching()
FileSystemWatcher1.EnableRaisingEvents = False
ListBox1.Items.Add("-----------------------------------------")
ListBox1.Items.Add("Überwachung wurde beendet.")
ListBox1.Items.Add(DateTime.Now.ToString)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "&Start" Then
If Not System.IO.Directory.Exists(TextBox1.Text) Then
TextBox1.Text = "C:\"
MessageBox.Show( _
"Bitte einen existierenden Pfad eingeben!", _
"Fehler - Abbruch", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Exit Sub
End If
StartWatching()
Button1.Text = "&Stop"
StatusBar1.Text = "Überwachung läuft..."
TextBox1.ReadOnly = True
TextBox2.ReadOnly = True
Else
StopWatching()
Button1.Text = "&Start"
StatusBar1.Text = "Überwachung angehalten"
Button4.Enabled = True
TextBox1.ReadOnly = False
TextBox2.ReadOnly = False
If CheckBox1.Checked Then
SaveListbox(ListBox1, sLogFile)
Process.Start(sLogFile)
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = GetFolder()
End Sub
Private Function GetFolder() As String
Dim FolderbrowserDialog1 As New FolderBrowserDialog
With FolderbrowserDialog1
.Description = "Wählen Sie einen Ordner."
.RootFolder = Environment.SpecialFolder.MyComputer
If .ShowDialog() = DialogResult.OK Then
Return FolderbrowserDialog1.SelectedPath()
Else
Return String.Empty
End If
End With
End Function
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
If Button1.Text = "&Stop" Then
If MessageBox.Show( _
"Soll das Programm wirklich beendet werden?" & _
vbNewLine & _
"Die Überwachung wird damit ebenfalls beendet.", _
"Beenden?", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question) <> DialogResult.Yes Then
e.Cancel = True
Else
StopWatching()
End If
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
SaveListbox(ListBox1, sLogFile)
Process.Start(sLogFile)
End Sub
Private Sub SaveListbox(ByVal ListBox As ListBox, _
ByVal sFile As String)
Dim stream As IO.StreamWriter
Dim i As Short
stream = New IO.StreamWriter(sFile)
For i = 0 To CShort(ListBox.Items.Count - 1)
stream.WriteLine(ListBox.Items(i))
Next
stream.Close()
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Button4.Visible = False
Else
Button4.Visible = True
End If
End Sub
End Class
|
|