【问题标题】:Copy File on Modification FIleSystemWatcher, Visual Basic (VS 2012 V11)复制文件修改 FIleSystemWatcher, Visual Basic (VS 2012 V11)
【发布时间】:2013-05-11 06:35:58
【问题描述】:

浏览了许多网站和一些教程视频后,我仍然对这个感到困惑。我正在完成一个程序,我需要添加最后一点功能。

程序以这种方式工作。用户在 textbox1 中指定一个文件,然后在 textbox2 中指定一个目录。用户通过在 textbox3 中复制文件来设置他们希望文件的频率。用户点击运行,程序将文件复制到新位置,每次复制时在文件名中添加一个数字(以避免覆盖)。一切正常,但我希望用户可以选择按时间复制文件或在文件被修改时复制。

如何使用 FileSystemWatcher 在目录中查找修改(在 textbox1 中给出),然后调用将指定目录复制到目标目标的语句(在 textbox 2 中指定)?

补充说明:

在一个教程中,通过这样做设置了 FileSystemWatcher 路径

Dim watched As String = System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures")
        Dim fsw As New FileSystemWatcher(watched)

代码指向的路径是 "C:\Users[User Name]\Pictures" 。 我在网上找不到显示“.GetEnvironmentVariable”接受哪些变量甚至变量含义的资源。这是我在使用最后一位代码时遇到问题的众多原因之一。

【问题讨论】:

  • 显然您对 GetEnvironmentVariable() 不感兴趣,请改用 Path.GetDirectoryName(TextBox1.Text)。当 Change 事件触发时,将更改的文件的名称与 TextBox2.Text 进行比较。最后一个问题是当文件更改时您经常无法复制文件,您需要使用 Timer 稍后再执行。

标签: vb.net directory copy filesystemwatcher


【解决方案1】:

GetEnvironmentVariable为当前进程返回指定环境的值。

在您的示例中,USERPROFILE 是当前用户的文件夹路径。例如,在我的笔记本电脑上,USERPROFILE 是 C:\Users\Tim。

System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures") 的输出将是 USERPROFILE 路径加上“图片” - 继续我的示例,它将是 C:\Users\Tim\Pictures - 这是我的用户帐户的“我的图片”文件夹的物理路径。

要获取所有环境变量的列表,如果您好奇,请转到 DOS 提示符并输入 SET 并按回车键。

要回答您的原始问题,您需要处理FileSystemWatcher.Changed Event

例如:

Private Shared Sub OnChanged(source As Object, e As RenamedEventArgs)

   ' Do your copy here
End Sub

您可以像这样将事件处理程序连接到您的FileWatcher

AddHandler fsw.Changed, AddressOf OnChanged

但是,请注意 MSDN 文档中的此警告。

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

【讨论】:

    【解决方案2】:

    这是我用它做的代码。

    Option Explicit On
    Option Strict On
    Imports System.IO
    Imports Microsoft.VisualBasic ' I don't know this one, but the code worked without this.
    Imports System.Security.Permissions ' I don't know the exactly what this does but the
      ' http://msdn.microsoft.com/ site did this, I assume it'f to allow for permission to 
      ' watch files? The code still worked for me without this
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim directoryPath As String = Path.GetDirectoryName(TextBox1.Text)
            Dim varFileSystemWatcher As New FileSystemWatcher()
            varFileSystemWatcher.Path = directoryPath
    
            varFileSystemWatcher.NotifyFilter = (NotifyFilters.LastWrite)
    
            varFileSystemWatcher.Filter = Path.GetFileName(TextBox1.Text) ' I know this
               ' limits what's being watched to one file, but right now this is 
               ' what I want the program to do.
    
            AddHandler varFileSystemWatcher.Changed, AddressOf OnChanged
    
    
            varFileSystemWatcher.EnableRaisingEvents = True
    
    
        End Sub
    
        Private Sub OnChanged(source As Object, ByVal e As FileSystemEventArgs)
    
            My.Computer.FileSystem.CopyFile(e.FullPath, TextBox2.Text & "\" & e.Name, True)
    
        End Sub
    

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多