Я нашел в сети скрипт для копирования некоторых файлов и отображения индикатора выполнения для пользователя. Я задавался вопросом, можно ли было автоматизировать этот процесс, чтобы копия файла запускалась сразу после запуска программы, а затем закрывалась после ее завершения. 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