-1

多くのサイトといくつかのチュートリアル ビデオを見た後、私はまだこれに困惑しています。プログラムを完成させようとしていて、最後にもう 1 つ機能を追加する必要があります。

プログラムはこのように動作します。ユーザーは textbox1 でファイルを指定し、textbox2 でディレクトリを指定します。ユーザーは、テキストボックス 3 にファイルをコピーする頻度を設定します。ユーザーが実行を押すと、プログラムはファイルを新しい場所にコピーし、コピーするたびにファイル名に番号を追加します (上書きを避けるため)。それはすべて正常に機能しますが、ファイルを時間ごとにコピーするか、ファイルが変更されたときにコピーするかをユーザーが選択できるようにしたいと考えています。

FileSystemWatcher を使用してディレクトリ (テキストボックス 1 で指定) の変更を探し、指定されたディレクトリをターゲットの宛先 (テキストボックス 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」が受け入れる変数や、変数の意味を示すリソースをオンラインで見つけることができません。これは、この最後のビット コードで問題が発生する多くの理由の 1 つです。

4

2 に答える 2

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
于 2013-05-11T15:14:20.097 に答える
0

GetEnvironmentVariable現在のプロセスの指定された環境の値を返します。

あなたの例の場合USERPROFILE、現在のユーザーのフォルダーへのパスです。たとえば、私のラップトップでは、USERPROFILE は C:\Users\Tim です。

の出力はSystem.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures")、USERPROFILE パスに「Pictures」を加えたものになります。私の例を続けると、C:\Users\Tim\Picturesこれは、私のユーザー アカウントの My Pictures フォルダーへの物理パスになります。

すべての環境変数のリストを取得するには、興味がある場合は、DOS プロンプトに移動して入力し、Enter キーを押します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.

于 2013-05-11T07:32:06.933 に答える