如何自动化这个VB脚本?

时间:2012-04-01 00:07:40

标签: windows vb.net visual-studio file-copying

我在线发现了一个脚本来复制一些文件,同时还向用户显示了一个进度条。我想知道是否可以自动执行此过程,以便文件复制在程序启动后立即启动,然后在完成后关闭。 VB不是我的强项,因为我通常用c sharp编码。任何帮助非常感谢。

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 个答案:

答案 0 :(得分:2)

只需将Button1_Click中的代码移动到Form_Load事件中即可。

或者,您也可以从Form_Load调用Button1_Click例程。

另外,仅供参考,一些针对C#程序员的VB.net技巧:

  • 您通常不需要在VB.net中执行动态AddHandler设置

  • 此动态设置通常由方法声明的静态 Handles 子句替换,如果您使用IDE Con​​trol / Event下拉列表,则会自动为您生成

  • 对于您的BackgroundWorker声明,您可能确实需要“Private With Events ...”。

答案 1 :(得分:0)