当前位置: 首页>>代码示例>>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;未经允许,请勿转载。