[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Copy system, hidden and open files

Posted on 2004-09-01
2
Medium Priority
?
312 Views
Last Modified: 2010-04-23
I need to copy system, hidden and open files (files currently in use by windows) , this is a part of a backup program.

I have tried to use shellcommand to ms-dos command copy and xcopy but i realised thats an ugly solution.

This must be possible, but i havent found a way to do this with Vb.net.
0
Comment
Question by:McLyd
2 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 1000 total points
ID: 11955421
Imports System.IO
Imports System.Threading
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
    End Sub
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        Me.Button1 = New System.Windows.Forms.Button
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(24, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Source"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(168, 24)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(24, 56)
        Me.Label2.Name = "Label2"
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Destination"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(168, 48)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.TabIndex = 3
        Me.TextBox2.Text = ""
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Location = New System.Drawing.Point(8, 240)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(272, 23)
        Me.ProgressBar1.Step = 1
        Me.ProgressBar1.TabIndex = 4
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(200, 80)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 5
        Me.Button1.Text = "Go"
        '
        'RichTextBox1
        '
        Me.RichTextBox1.Location = New System.Drawing.Point(16, 112)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(264, 120)
        Me.RichTextBox1.TabIndex = 6
        Me.RichTextBox1.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 270)
        Me.Controls.Add(Me.RichTextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ProgressBar1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub updateprogress(ByVal progress As Integer)
        If (progress < Me.ProgressBar1.Maximum AndAlso progress > Me.ProgressBar1.Minimum) Then
            Me.ProgressBar1.Value = progress
        End If
    End Sub
    Private Sub showmessage(ByVal msg As String)
        Me.RichTextBox1.AppendText(msg & vbCrLf)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim foo As New FileCopy
        AddHandler foo.CopyProgress, AddressOf Me.updateprogress
        AddHandler foo.Message, AddressOf Me.showmessage
        foo.Copy(Me.TextBox1.Text, Me.TextBox2.Text)
    End Sub
End Class
Public Class FileCopy
    Private Class CreateDir
        Public DirectoryName As String
        Public Sub New(ByVal _DirectoryName As String)
            Me.DirectoryName = _DirectoryName
        End Sub
    End Class
    Private Class CopyFile
        Public Source As String
        Public Dest As String
        Public Sub New(ByVal _source As String, ByVal _dest As String)
            Me.Source = _source
            Me.Dest = _dest
        End Sub
    End Class
    Private Directories As ArrayList = New ArrayList 'contains directories to be created
    Private Files As ArrayList = New ArrayList ' contains files to be copied
    Public Event CopyProgress(ByVal progress As Integer)
    Private Sub UpdateProgress(ByVal progress As Integer)
        RaiseEvent CopyProgress(progress)
    End Sub
    Public Event Message(ByVal msg As String)
    Private Sub DoMessage(ByVal msg As String)
        RaiseEvent Message(msg)
    End Sub
    Private Sub CreateDirectories()
        Dim i As Integer
        For i = 0 To Me.Directories.Count - 1
            'create the dir
            Dim dir As CreateDir = DirectCast(Me.Directories(i), CreateDir)
            Me.DoMessage("Creating directory " & dir.DirectoryName)
            Directory.CreateDirectory(dir.DirectoryName)
        Next
    End Sub
    Private Sub CopyFiles()
        Dim i As Integer
        For i = 0 To Me.Files.Count - 1
            Dim cp As CopyFile = DirectCast(Me.Files(i), CopyFile)
            Me.DoMessage("Copying " & cp.Source & " to " & cp.Dest)
            Me.UpdateProgress((CDbl(i + 1) / CDbl(Me.Files.Count)) * 100)
            File.Copy(cp.Source, cp.Dest)
        Next
    End Sub
    Private Sub PrepareInfo(ByVal src As String, ByVal dest As String)
        Try
            Dim tododir As CreateDir = New CreateDir(dest)
            Me.Directories.Add(tododir) 'add it for later
            Dim d As DirectoryInfo = New DirectoryInfo(src)
            Dim f As FileInfo() = d.GetFiles()
            Dim file As FileInfo
            For Each file In d.GetFiles()
                Dim todofile As CopyFile = New CopyFile(file.FullName, dest + "\" + file.Name)
                Me.Files.Add(todofile)
            Next
            Dim folder As DirectoryInfo
            For Each folder In d.GetDirectories() ' take all folders
                PrepareInfo(src + "\" + folder.Name, dest + "\" + folder.Name) 'recursive function calling itself  
            Next
        Catch ex As Exception
            Throw New System.Exception("Unable to copy", ex)
        End Try
    End Sub
    Public Sub Copy(ByVal src As String, ByVal dest As String)
        Try
            '1 get the info
            Me.DoMessage("Building work")
            Me.PrepareInfo(src, dest)
            Me.DoMessage("There are " & Me.Directories.Count & " directories being created")
            Me.DoMessage("There are " & Me.Files.Count & " files being copied")
            '2 actually do the work
            Me.CreateDirectories()
            Me.CopyFiles()
            Me.DoMessage("Complete")
        Catch Ex As Exception
            MessageBox.Show(Ex.ToString())
        End Try
    End Sub
End Class

 
I would further recommend running Copy in a seperate thread but I think there is enough here to get you pretty far along.
 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11955644
Bravo, bravissimo, and the crowd goes wild...

Bob
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question