當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET FileSystemWatcher.Renamed事件代碼示例

本文整理匯總了VB.NET中System.IO.FileSystemWatcher.Renamed事件的典型用法代碼示例。如果您正苦於以下問題:VB.NET FileSystemWatcher.Renamed事件的具體用法?VB.NET FileSystemWatcher.Renamed怎麽用?VB.NET FileSystemWatcher.Renamed使用的例子?那麽, 這裏精選的事件代碼示例或許可以為您提供幫助。您也可以進一步了解該事件所在System.IO.FileSystemWatcher的用法示例。


在下文中一共展示了FileSystemWatcher.Renamed事件的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Watcher

' 導入命名空間
Imports System.IO
Imports System.Security.Permissions

Public Class Watcher

    Public Shared Sub Main()

        Run()

    End Sub

    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
    Private Shared Sub Run()

        Dim args() As String = Environment.GetCommandLineArgs()

        ' If a directory is not specified, exit the program.
        If args.Length <> 2 Then
            ' Display the proper way to call the program.
            Console.WriteLine("Usage: Watcher.exe (directory)")
            Return
        End If

        ' Create a new FileSystemWatcher and set its properties.
        Using watcher As New FileSystemWatcher()
            watcher.Path = args(1)

            ' 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 text files.
            watcher.Filter = "*.txt"

            ' 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

            ' Wait for the user to quit the program.
            Console.WriteLine("Press 'q' to quit the sample.")
            While Chr(Console.Read()) <> "q"c
            End While
        End Using
    End Sub

    ' Define the event handlers.
    Private Shared Sub OnChanged(source As Object, 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(source As Object, e As RenamedEventArgs)
        ' Specify what is done when a file is renamed.
        Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}")
    End Sub

End Class
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:66,代碼來源:FileSystemWatcher.Renamed

示例2: FileSystemWatcherDemo

' 導入命名空間
Imports System.IO

Imports System.Windows.Forms

public class FileSystemWatcherDemo
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtPath As System.Windows.Forms.TextBox
    Friend WithEvents txtFile As System.Windows.Forms.TextBox
    Friend WithEvents txtWatcher As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents btnWatch As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.txtPath = New System.Windows.Forms.TextBox()
        Me.txtFile = New System.Windows.Forms.TextBox()
        Me.txtWatcher = New System.Windows.Forms.TextBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.btnWatch = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label1.Location = New System.Drawing.Point(51, 20)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(103, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "File Watcher"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label2.Location = New System.Drawing.Point(51, 52)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(103, 16)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "File Type"
        '
        'txtPath
        '
        Me.txtPath.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtPath.Location = New System.Drawing.Point(164, 16)
        Me.txtPath.Name = "txtPath"
        Me.txtPath.Size = New System.Drawing.Size(655, 23)
        Me.txtPath.TabIndex = 2
        Me.txtPath.Text = "C:\Temp\test\"
        '
        'txtFile
        '
        Me.txtFile.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtFile.Location = New System.Drawing.Point(164, 48)
        Me.txtFile.Name = "txtFile"
        Me.txtFile.Size = New System.Drawing.Size(133, 23)
        Me.txtFile.TabIndex = 3
        Me.txtFile.Text = "*.txt"
        '
        'txtWatcher
        '
        Me.txtWatcher.Location = New System.Drawing.Point(51, 104)
        Me.txtWatcher.Multiline = True
        Me.txtWatcher.Name = "txtWatcher"
        Me.txtWatcher.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtWatcher.Size = New System.Drawing.Size(778, 224)
        Me.txtWatcher.TabIndex = 5
        Me.txtWatcher.Text = ""
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label3.Location = New System.Drawing.Point(51, 80)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(103, 16)
        Me.Label3.TabIndex = 6
        Me.Label3.Text = "Message"
        '
        'btnWatch
        '
        Me.btnWatch.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnWatch.Location = New System.Drawing.Point(625, 64)
        Me.btnWatch.Name = "btnWatch"
        Me.btnWatch.Size = New System.Drawing.Size(194, 32)
        Me.btnWatch.TabIndex = 7
        Me.btnWatch.Text = "Start"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(870, 357)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWatch, Me.Label3, Me.txtWatcher, Me.txtFile, Me.txtPath, Me.Label2, Me.Label1})
        Me.ResumeLayout(False)

    End Sub

    Dim WithEvents FSWatcher As New FileSystemWatcher()

    Private Sub btnWatch_Click _
      (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatch.Click
        Dim strPathWatch As String
        Dim strFileWatch As String
        strPathWatch = txtPath.Text
        strFileWatch = txtFile.Text
        FSWatcher.Path = strPathWatch
        FSWatcher.Filter = strFileWatch
        FSWatcher.IncludeSubdirectories = True
        FSWatcher.NotifyFilter = _
          NotifyFilters.LastWrite Or NotifyFilters.FileName
        FSWatcher.EnableRaisingEvents = True
        txtWatcher.Text += vbCrLf + "Start:"

    End Sub

    Private Sub FSWatcher_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Changed
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub FSWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Created
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub FSWatcher_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Deleted
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub FSWatcher_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs) Handles FSWatcher.Error
        WatchMessage("", e.GetException.Message)
    End Sub


    Private Sub FSWatcher_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FSWatcher.Renamed
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub WatchMessage(ByVal strpFileName As String, ByVal strpMessage As String)
        Dim strMessage As String
        strMessage =  strpFileName + " " + strpMessage
        txtWatcher.Text += vbCrLf + strMessage

    End Sub
End Class
開發者ID:VB程序員,項目名稱:System.IO,代碼行數:167,代碼來源:FileSystemWatcher.Renamed


注:本文中的System.IO.FileSystemWatcher.Renamed事件示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。