2012-04-01 8 views
1

いくつかのファイルをコピーするためにオンラインのスクリプトが見つかりましたが、プログレスバーも表示されていました。このプロセスを自動化することが可能かどうか疑問に思ったので、プログラムの起動後すぐにファイルのコピーが開始され、完了すると閉じられます。 VBは私が通常Cシャープでコードしているので、私の強みではありません。どんな助けでも大歓迎です。このVBスクリプトを自動化するにはどうすればよいですか?

Imports System.Collections.Generic 
Imports System.ComponentModel 
Imports System.IO 

Public Class Form1 

    '--- Class to report progress 
    Private Class UIProgress 
     Public Sub New(ByVal name_ As String, ByVal bytes_ As Long, ByVal maxbytes_ As Long) 
      name = name_ : bytes = bytes_ : maxbytes = maxbytes_ 
     End Sub 
     Public name As String 
     Public bytes As Long 
     Public maxbytes As Long 
    End Class 

    '--- Class to report exception 
    Private Class UIError 
     Public Sub New(ByVal ex As Exception, ByVal path_ As String) 
      msg = ex.Message : path = path_ : result = DialogResult.Cancel 
     End Sub 
     Public msg As String 
     Public path As String 
     Public result As DialogResult 
    End Class 

    '--- Members 
    Private mCopier As New BackgroundWorker 
    Private Delegate Sub ProgressChanged(ByVal info As UIProgress) 
    Private Delegate Sub CopyError(ByVal err As UIError) 
    Private OnChange As ProgressChanged 
    Private OnError As CopyError 

    Public Sub New() 
     InitializeComponent() 
     AddHandler mCopier.DoWork, AddressOf Copier_DoWork 
     AddHandler mCopier.RunWorkerCompleted, AddressOf Copier_RunWorkerCompleted 
     mCopier.WorkerSupportsCancellation = False 
     OnChange = AddressOf Copier_ProgressChanged 
     OnError = AddressOf Copier_Error 
     ChangeUI(False) 
     mCopier.RunWorkerAsync() 

    End Sub 

    Private Sub Copier_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) 
     '--- Create list of files to copy 
     Dim theExtensions As String() = {"*.jpg", "*.jpeg", "*.bmp", "*.png", "*.gif"} 
     Dim files As New List(Of FileInfo) 
     Dim path As String = "c:\users\simon\desktop\b\" 
     Dim dir As New DirectoryInfo(path) 
     Dim maxbytes As Long = 0 
     For Each ext As String In theExtensions 
      Dim folder As FileInfo() = dir.GetFiles(ext, SearchOption.AllDirectories) 
      For Each file As FileInfo In folder 
       If ((file.Attributes And FileAttributes.Directory) <> 0) Then Continue For 
       files.Add(file) 
       maxbytes += file.Length 
      Next 
     Next 
     '--- Copy files 
     Dim bytes As Long = 0 
     For Each file As FileInfo In files 
      Try 
       Me.BeginInvoke(OnChange, New Object() {New UIProgress(file.Name, bytes, maxbytes)}) 
       System.IO.File.Copy(file.FullName, "C:\Users\Simon\Desktop\t\" + file.Name, True) 
      Catch ex As Exception 
       Dim err As New UIError(ex, file.FullName) 
       Me.Invoke(OnError, New Object() {err}) 
       If err.result = DialogResult.Cancel Then Exit For 
      End Try 
      bytes += file.Length 
     Next 
    End Sub 

    Private Sub Copier_ProgressChanged(ByVal info As UIProgress) 
     '--- Update progress 
     ProgressBar1.Value = CInt(100.0 * info.bytes/info.maxbytes) 
     Label1.Text = "Copying " + info.name 
    End Sub 

    Private Sub Copier_Error(ByVal err As UIError) 
     '--- Error handler 
     Dim msg As String = String.Format("Error copying file {0}\n{1}\nClick OK to continue copying files", Err.path, Err.msg) 
     err.result = MessageBox.Show(msg, "Copy error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) 
    End Sub 

    Private Sub Copier_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) 
     '--- Operation completed, update UI 
     ChangeUI(False) 
    End Sub 

    Private Sub ChangeUI(ByVal docopy As Boolean) 
     Label1.Visible = docopy 
     ProgressBar1.Visible = docopy 
     If docopy Then Button1.Enabled = False Else Button1.Text = "Copy" 
     Label1.Text = "Starting copy..." 
     ProgressBar1.Value = 0 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim docopy As Boolean = Button1.Text = "Copy" 
     ChangeUI(docopy) 
     If (docopy) Then mCopier.RunWorkerAsync() Else mCopier.CancelAsync() 
    End Sub 

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click 

    End Sub 
End Class 

答えて

2

ちょうどあなたがForm_LoadイベントにのButton1 Clickで持っているコードを移動します。

または、Form_LoadからButton1_Clickルーチンを呼び出すことができます。また

は、FYI、C#のプログラマのためのいくつかのVB.netのヒント:

  • あなたは、一般的にVB.net

  • でダイナミックAddHandlerのセットアップを行う必要はありません。このダイナミックな設定は、通常、 IDEコントロール/イベントのドロップダウンを使用する場合は、メソッドの宣言のハンドル句に置き換えられます。

  • 「プライベートWith Events ... "という名前のBackgroundWorker宣言があります。

関連する問題