VB.Net/File Directory/File System Watcher — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:45, 26 мая 2010
File System Watcher: File Folder Created
Imports System
Imports System.IO
Public Class MainClass
Shared Private WithEvents m_FileSystemWatcher As FileSystemWatcher
Shared Sub Main()
m_FileSystemWatcher = New FileSystemWatcher("C:\")
m_FileSystemWatcher.EnableRaisingEvents = True
End Sub
" Watch for file creations.
Shared Private Sub m_FileSystemWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles m_FileSystemWatcher.Created
Console.WriteLine(e.FullPath & " created")
End Sub
End Class
File System Watcher: file updated, created
Imports System.IO
Module Module1
WithEvents Watcher As New FileSystemWatcher("C:\")
Sub Main()
Watcher.Filter = "*.*"
Watcher.IncludeSubdirectories = True
Watcher.EnableRaisingEvents = True
Console.ReadLine()
End Sub
Sub OnChanged(ByVal From As Object, ByVal e As FileSystemEventArgs) Handles Watcher.Changed
Dim DateAndTime As DateTime = DateTime.Now
Console.WriteLine(e.FullPath & " " _
& e.ChangeType() & " " & DateAndTime)
End Sub
End Module
File Watcher
Imports System.IO
Imports System.Windows.Forms
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Watcher
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label2 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.txtTypes = New System.Windows.Forms.TextBox
Me.lstNewFiles = New System.Windows.Forms.ListBox
Me.cmdStop = New System.Windows.Forms.Button
Me.cmdStart = New System.Windows.Forms.Button
Me.txtDir = New System.Windows.Forms.TextBox
Me.SuspendLayout()
"
"Label2
"
Me.Label2.Location = New System.Drawing.Point(16, 40)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(60, 12)
Me.Label2.TabIndex = 13
Me.Label2.Text = "File Types:"
"
"Label1
"
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(40, 12)
Me.Label1.TabIndex = 12
Me.Label1.Text = "Path:"
"
"txtTypes
"
Me.txtTypes.Location = New System.Drawing.Point(76, 36)
Me.txtTypes.Name = "txtTypes"
Me.txtTypes.Size = New System.Drawing.Size(264, 21)
Me.txtTypes.TabIndex = 11
Me.txtTypes.Text = "*.xls"
"
"lstNewFiles
"
Me.lstNewFiles.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.lstNewFiles.Location = New System.Drawing.Point(12, 108)
Me.lstNewFiles.Name = "lstNewFiles"
Me.lstNewFiles.Size = New System.Drawing.Size(328, 199)
Me.lstNewFiles.TabIndex = 10
"
"cmdStop
"
Me.cmdStop.Location = New System.Drawing.Point(228, 68)
Me.cmdStop.Name = "cmdStop"
Me.cmdStop.Size = New System.Drawing.Size(112, 24)
Me.cmdStop.TabIndex = 9
Me.cmdStop.Text = "Stop Watching"
"
"cmdStart
"
Me.cmdStart.Location = New System.Drawing.Point(108, 68)
Me.cmdStart.Name = "cmdStart"
Me.cmdStart.Size = New System.Drawing.Size(112, 24)
Me.cmdStart.TabIndex = 8
Me.cmdStart.Text = "Start Watching"
"
"txtDir
"
Me.txtDir.Location = New System.Drawing.Point(76, 12)
Me.txtDir.Name = "txtDir"
Me.txtDir.Size = New System.Drawing.Size(264, 21)
Me.txtDir.TabIndex = 7
Me.txtDir.Text = "d:\orders"
"
"Watcher
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(352, 318)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtTypes)
Me.Controls.Add(Me.lstNewFiles)
Me.Controls.Add(Me.cmdStop)
Me.Controls.Add(Me.cmdStart)
Me.Controls.Add(Me.txtDir)
Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "Watcher"
Me.Text = "Watcher"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtTypes As System.Windows.Forms.TextBox
Friend WithEvents lstNewFiles As System.Windows.Forms.ListBox
Friend WithEvents cmdStop As System.Windows.Forms.Button
Friend WithEvents cmdStart As System.Windows.Forms.Button
Friend WithEvents txtDir As System.Windows.Forms.TextBox
Private WithEvents Watch As New FileSystemWatcher()
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
Watch.Path = txtDir.Text
Watch.Filter = txtTypes.Text
Watch.EnableRaisingEvents = True
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
Watch.EnableRaisingEvents = False
End Sub
Private Sub Watch_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles Watch.Created
lstNewFiles.Invoke(New UpdateListDelegate(AddressOf UpdateList), e.Name)
End Sub
Private Delegate Sub UpdateListDelegate(ByVal name As String)
Private Sub UpdateList(ByVal name As String)
lstNewFiles.Items.Add("New file: " & Name)
End Sub
End Class