Click here to Skip to main content
15,892,298 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: Help in C# certificat of appreciation Pin
holal17-Apr-09 22:39
holal17-Apr-09 22:39 
GeneralRe: Help in C# certificat of appreciation Pin
Eddy Vluggen18-Apr-09 0:14
professionalEddy Vluggen18-Apr-09 0:14 
GeneralRe: Help in C# certificat of appreciation Pin
holal22-Apr-09 23:19
holal22-Apr-09 23:19 
GeneralRe: Help in C# certificat of appreciation Pin
Eddy Vluggen22-Apr-09 23:49
professionalEddy Vluggen22-Apr-09 23:49 
QuestionHow to copy some files from one folder to other with multithreading ? Pin
Hadi Basiri16-Apr-09 14:05
Hadi Basiri16-Apr-09 14:05 
AnswerRe: How to copy some files from one folder to other with multithreading ? Pin
Luc Pattyn16-Apr-09 14:20
sitebuilderLuc Pattyn16-Apr-09 14:20 
AnswerRe: Ignore! Pin
Henry Minute17-Apr-09 2:15
Henry Minute17-Apr-09 2:15 
Questioncopy all files from source folder or drive too destination file or drive useing vb 2005 Pin
Member 444478316-Apr-09 12:29
Member 444478316-Apr-09 12:29 
I,m working on a project after searching the web I found a copy class that I have modified the code to meet my need,s when I,m done this will be a backup utility My copy class does a good job of coping file but for me there is one flaw I does not copy every file files that are locked or files in use by another process and it does not copy empty folders this is what I would like to do Is have my copy class copy all file and folers one paticular folder I would like to see it copy. Is all of the window\system32 and all its files. some one please help



Here is my copy class code:

Imports System.Threading
Imports System.IO
Imports System.Security.Permissions.FileIOPermissionAccess

Public Class CopyClass
    'This will hold the reference to the client form
    Private _clientApp As Form
    'Create a delegate method that will map to the CopyThreadMessage method of the client app
    Private Delegate Sub CallClientCopy(ByVal ThreadName As String, ByVal FilesRemaining As Long, ByVal Message As String)
    'Create a delegate method that will map to the CountThreadMessage method of the client app
    Private Delegate Sub CallClientCount(ByVal ThreadName As String, ByVal TotalFiles As Long, ByVal TotalFolders As Long, ByVal Files As Long, ByVal Message As String)

    'Create an object for each deletegate
    Private _callClientCopy As CallClientCopy
    Private _callClientCount As CallClientCount

    ' Property variables
    Private _firstTime As Boolean
    Private _fromPath As String
    Private _toPath As String
    Private _directories As Long
    Private _files As Long
    Private _copiedFiles As Long
    Private _totalFiles As Long
    Private _fileName As String
    ' Constants
    Private Const LOG_FILE As String = "BackupLog.txt"

    Public Sub New(ByRef ClientApp As Backup)
        ' Save the reference to the client app
        _clientApp = ClientApp
        ' Assign delegate objects
        _callClientCopy = AddressOf ClientApp.CopyThreadMessage
        '_callClientCopy = AddressOf ClientApp.CopyThreadMessage
        _callClientCount = AddressOf ClientApp.CountThreadMessage
    End Sub
    Public Sub CopyFiles()
        'Do the work of the first thread here
        ' Give this thread a name
        If Thread.CurrentThread.Name = Nothing Then Thread.CurrentThread.Name = "Copy"
        ' Create a new DirectoryInfo object for from path.
        Dim dir As New DirectoryInfo(FromPath)
        ' Call the GetFileSystemInfos method.
        Dim FSinfo As FileSystemInfo() = dir.GetFileSystemInfos
        'Copy one file at a time looping until all files are copied
        ReallyCopyFiles(FSinfo)
        'Call client one last time to signal end of copy
        CallClient(Thread.CurrentThread.Name, _copiedFiles, _totalFiles, _directories, "END")
    End Sub

    Public Sub GetCountData()
        'Do the work of the second thread here
        ' Give this thread a name
        If Thread.CurrentThread.Name = Nothing Then Thread.CurrentThread.Name = "Count"
        ' Create a new DirectoryInfo object for from path.
        Dim dir As New DirectoryInfo(FromPath)
        ' Call the GetFileSystemInfos method.
        Dim FSinfo As FileSystemInfo() = dir.GetFileSystemInfos
        ' Count folder and files
        CountFiles(FSinfo)
        ' Save total files count
        _totalFiles = _files
        ' Send message to client form
        CallClient(Thread.CurrentThread.Name, _files, _totalFiles, _directories, "END")
    End Sub

    Private Sub ReallyCopyFiles(ByVal FSInfo As FileSystemInfo())

        ' Check the FSInfo parameter.
        If FSInfo Is Nothing Then
            Throw New ArgumentNullException("FSInfo")
        End If
        ' Iterate through each item.
        Dim i As FileSystemInfo
        For Each i In FSInfo
            Try
                ' Check to see if this is a DirectoryInfo object.
                If TypeOf i Is DirectoryInfo Then
                    ' Cast the object to a DirectoryInfo object.
                    Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)
                    ' Iterate (recurse) through all sub-directories.
                    ReallyCopyFiles(dInfo.GetFileSystemInfos())
                    ' Check to see if this is a FileInfo object.
                ElseIf TypeOf i Is FileInfo Then
                    'save the full path and file name
                    _fileName = i.FullName
                    'Get the copy path name only
                    Dim copypath As String = ToPath & Mid(_fileName, Len(FromPath) + 1, Len(_fileName) - Len(FromPath) - Len(i.Name))
                    'Create copy path if it does not exist

                    If Not Directory.Exists(copypath) Then
                        Directory.CreateDirectory(copypath)
                    End If
                    ' Get the to path and filename
                    Dim tofile As String = ToPath & Mid(_fileName, Len(FromPath) + 1)
                    ' Update status info on client
                    Dim fi As New FileInfo(_fileName)
                    'Dim Message As String = _directories & fi.Name
                    Dim Message As String = fi.Name '& " is " & Decimal.Round(CDec(fi.Length / 1048576), 2) & "MB in length."


                    CallClient(Thread.CurrentThread.Name, _copiedFiles, _totalFiles, _directories, Message)
                    ' if file exists check if file has been updated since last copy
                    Dim OkayToCopy As Boolean = True
                    If File.Exists(tofile) Then
                        If File.GetLastWriteTime(_fileName) = File.GetLastWriteTime(tofile) Then
                            OkayToCopy = False
                        End If
                    End If
                    ' Copy file with overwrite
                    If OkayToCopy Then File.Copy(_fileName, tofile, True)
                    ' Increment copied file count
                    _copiedFiles += 1
                End If
            Catch ex As Exception
                ' Report error but continue processing
                WriteLog(ex.Message.ToString)
            End Try
        Next i
    End Sub
    Private Sub CountFiles(ByVal FSInfo As FileSystemInfo())
        Static ShowCount As Long = 0
        ' Check the FSInfo parameter.
        If FSInfo Is Nothing Then
            Throw New ArgumentNullException("FSInfo")
        End If
        ' Iterate through each item.
        Dim i As FileSystemInfo
        For Each i In FSInfo
            Try
                ' Check to see if this is a DirectoryInfo object.
                If TypeOf i Is DirectoryInfo Then
                    ' Add one to the directory count.
                    _directories += 1
                    ' Cast the object to a DirectoryInfo object.
                    Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)
                    ' Iterate (recurse) through all sub-directories.
                    CountFiles(dInfo.GetFileSystemInfos())
                    ' Check to see if this is a FileInfo object.
                ElseIf TypeOf i Is FileInfo Then
                    ' Add one to the file count.
                    _files += 1
                    'display count for first file in every folder then every 200 - for faster performance
                    Select Case ShowCount
                        Case 0
                            ' Display count
                            CallClient(Thread.CurrentThread.Name, _files, _totalFiles, _directories, "")
                        Case Is >= 200
                            ' Display count
                            CallClient(Thread.CurrentThread.Name, _files, _totalFiles, _directories, "")
                            'reset so display is every 200 files in folder
                            ShowCount = 0
                    End Select
                    'Increment show count
                    ShowCount += 1
                End If
            Catch ex As Exception
                'Record error then continue (like a resume next)
                WriteLog(ex.Message.ToString)
            End Try
        Next i
    End Sub
    Private Sub CallClient(ByVal ThreadName As String, ByVal Files As Long, ByVal TotalFiles As Long, ByVal Directories As Long, ByVal Message As String)
        Select Case ThreadName
            Case "Copy"
                'Call the delegated method
                _clientApp.Invoke(_callClientCopy, ThreadName, Files, Message)



            Case "Count"
                'Call the delegated method
                _clientApp.Invoke(_callClientCount, ThreadName, Files, TotalFiles, Directories, Message)
        End Select
        'Let the thread sleep before continuing so the client app will have time to be process (1 millisecond is enough)
        Thread.Sleep(0)
    End Sub
    Private Sub WriteLog(ByVal Message As String)
        ' Create log file
        If Not File.Exists(LOG_FILE) Then
            Using sw As StreamWriter = File.CreateText(LOG_FILE)
                sw.WriteLine("BACKUP LOG FILE STARTED AT: " & DateTime.Now)
                sw.WriteLine("================================================")
                sw.WriteLine()
                sw.Close()
            End Using
        End If
        ' Create an instance of StreamWriter to write text to a file.
        Using sw As StreamWriter = New StreamWriter(LOG_FILE)
            ' Add some text to the file.
            sw.WriteLine()
            sw.WriteLine("TIME OF LOG ENTRY: " & DateTime.Now)
            ' Arbitrary objects can also be written to the file.
            sw.WriteLine(Message)
            sw.Close()
        End Using
    End Sub
    Private Property FirstTime() As Boolean
        Get
            Return _firstTime
        End Get
        Set(ByVal value As Boolean)
            _firstTime = value
        End Set
    End Property
    Public Property FromPath() As String
        Get
            Return _fromPath
        End Get
        Set(ByVal value As String)
            _fromPath = value
        End Set
    End Property
    Public Property ToPath() As String
        Get
            Return _toPath
        End Get
        Set(ByVal value As String)
            _toPath = value
        End Set
    End Property
    Public ReadOnly Property Directories() As Long
        Get
            Return _directories
        End Get
    End Property
    Public ReadOnly Property Files() As Long
        Get
            Return _files
        End Get
    End Property
    Public ReadOnly Property TotalFiles() As Long
        Get
            Return _totalFiles
        End Get
    End Property
End Class

AnswerRe: copy all files from source folder or drive too destination file or drive useing vb 2005 sorry Pin
Member 444478316-Apr-09 16:28
Member 444478316-Apr-09 16:28 
QuestionHow to use digital camera in dual mode using C# program and physically using shutter release button Pin
vsaratkar16-Apr-09 10:41
vsaratkar16-Apr-09 10:41 
AnswerRe: How to use digital camera in dual mode using C# program and physically using shutter release button Pin
Dave Kreskowiak16-Apr-09 12:54
mveDave Kreskowiak16-Apr-09 12:54 
GeneralRe: How to use digital camera in dual mode using C# program and physically using shutter release button Pin
vsaratkar20-Apr-09 2:25
vsaratkar20-Apr-09 2:25 
QuestionDerived PictureBox and OnKeyPress Events Pin
jensph16-Apr-09 9:57
jensph16-Apr-09 9:57 
AnswerRe: Derived PictureBox and OnKeyPress Events Pin
Henry Minute16-Apr-09 12:10
Henry Minute16-Apr-09 12:10 
GeneralRe: Derived PictureBox and OnKeyPress Events Pin
jensph16-Apr-09 12:39
jensph16-Apr-09 12:39 
GeneralRe: Derived PictureBox and OnKeyPress Events Pin
Luc Pattyn16-Apr-09 13:26
sitebuilderLuc Pattyn16-Apr-09 13:26 
QuestionNGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
dyonik16-Apr-09 3:39
dyonik16-Apr-09 3:39 
AnswerRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
Dave Kreskowiak16-Apr-09 4:27
mveDave Kreskowiak16-Apr-09 4:27 
GeneralRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
dyonik16-Apr-09 4:42
dyonik16-Apr-09 4:42 
GeneralRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
led mike16-Apr-09 5:40
led mike16-Apr-09 5:40 
GeneralRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
Dave Kreskowiak16-Apr-09 6:36
mveDave Kreskowiak16-Apr-09 6:36 
QuestionRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
led mike16-Apr-09 10:13
led mike16-Apr-09 10:13 
AnswerRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
Dave Kreskowiak16-Apr-09 10:31
mveDave Kreskowiak16-Apr-09 10:31 
GeneralRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
led mike16-Apr-09 11:39
led mike16-Apr-09 11:39 
GeneralRe: NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies Pin
dyonik16-Apr-09 22:30
dyonik16-Apr-09 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.