2013-05-11 49 views
-1

通過瀏覽很多網站和一些教程視頻後,我仍然難住這一個。我正在完成一個程序,我需要添加最後一個功能。複製文件在修改FIleSystemWatcher,Visual Basic(VS 2012 V11)

程序以這種方式工作。用戶在文本框1中指定一個文件,然後在文本框2中指定一個目錄。用戶通過複製到文本框3來設置他們想要文件的頻率。用戶點擊運行,程序將文件複製到新位置,每次複製文件名時都會添加一個數字(以避免覆蓋)。這一切都可以正常工作,但我希望用戶可以選擇按時間複製文件或修改文件。

如何使用FileSystemWatcher查找目錄中的修改(在textbox1中給出),然後調用將指定目錄複製到目標目標(在文本框2中指定)的語句?

附加註釋:

在一個教程FileSystemWatcher的路徑通過這樣做

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

,該代碼指示所述的路徑設置爲「C:\用戶[用戶名] \圖片」 。 我無法在網上找到顯示變量「.GetEnvironmentVariable」接受的變量,甚至變量的含義。這是我最後一位代碼遇到問題的很多原因之一。

+0

顯然你對GetEnvironmentVariable()不感興趣,而是使用Path.GetDirectoryName(TextBox1.Text)。當更改事件觸發時,比較根據TextBox2.Text更改的文件的名稱。最後一個問題是,當文件發生更改時,您經常無法複製該文件,您需要稍後使用計時器來執行此操作。 – 2013-05-11 11:44:16

回答

0

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.

0

這是我用它做什麼,我希望它的代碼。

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