Help needed for beginner

I am writing a program, of which part is to copy all files, folders and subfolders from a provided source location to destination.

When I run the code as shown below, if there are more than 2 subfolders in depth, or the source is on a CD then I recieve the following error:

"An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll."

"Additional information: Access to the path 'C:\New Folder\Autorun.inf' is denied"

I do have NTFS permissions to the folder and I am the local admin. Does anyone know why this is happening?strsrcis D:\ (a win2k cd) andstrdest is C:\new folder.

I hope someone can help because I am so confused by this and have spent hours trying to resolve this! Please help somebody!!

Many thanks in advance.

Paul

code is here

' This sub is used to copy all files and subfolders in a folder to a specified destination

' The sub is written in this way, to ensure that we dont hammer the CPU during the process of copying all of the files

PublicSub CopyDirectory(ByVal strsrcAsString,ByVal strdestAsString)

Dim dirInfoActualAs IO.DirectoryInfo =New IO.DirectoryInfo(strsrc)

Dim fsInfoAs IO.FileInfo

Dim fsname

Dim dirInfoAs IO.DirectoryInfo

ForEach fsInfoIn dirInfoActual.GetFiles()

fsInfo.CopyTo(IO.Path.Combine(strdest, fsInfo.Name),True)

System.Threading.Thread.Sleep(500)' Sleep for 0.5 second

'To do: progress bar run here

ForEach dirInfoIn dirInfoActual.GetDirectories()

Dim subDirectoryAsString = IO.Path.Combine(strdest, dirInfo.Name)

IO.Directory.CreateDirectory(subDirectory)

CopyDirectory(dirInfo.FullName, subDirectory)

Next

Next

EndSub

[3772 byte] By [pauledavey] at [2007-12-28]
# 1
This is probably unrelated to your problem, but the way you set up

those loops seems wrong. The way I see it, you are looping

through the subdirectories more than once.

hgen_banks at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

hgen_banks is probably right. You're copying all the directories several times - as many times as you have files in the top level folder.

Since you're copying off CD, the files are being copied off read-only (since a CD is a readonly file system). The second time you try to copy all the directories again, you're trying to overwrite readonly files - which of course fails.

-HTH

geoff.appleby at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Couls someone show me how this piece of code should look please? someone else helped me right it and he isn't around now. I thought I understood it but I have my knickers in a twist somewhere. What is the bit that needs correcting, and what should the code be?

thanks again for your help guys.

Paul

pauledavey at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Guys, I think I figured this one out. I have changed the code as below. Is this the best way of doing it?

For Each fsInfo In dirInfoActual.GetFiles()

fsInfo.CopyTo(IO.Path.Combine(strdest, fsInfo.Name), True)

System.Threading.Thread.Sleep(500) ' Sleep for 0.5 second

'To do: progress bar run here

Next

For Each dirInfo In dirInfoActual.GetDirectories()

Dim subDirectory As String = IO.Path.Combine(strdest, dirInfo.Name)

IO.Directory.CreateDirectory(subDirectory)

CopyDirectory(dirInfo.FullName, subDirectory)

'Next

Next

pauledavey at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
The other thing that I have noticed is that this code copies files really slowly. Backing up the Windows 2000 CD to your hard disk takes a lot longer using this method than simply doing it manually in windows explorer. Why is this? Is there anything I cando to speed this up?
pauledavey at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

You are sleeping for half a second after each file copy, it looks like...

SJWhiteley at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7

Thanks SJWhiteley.. doh! Even with my limited experience I should have seen that one. Ok this is all starting to make sense now.

So the only question I have left now, is that the code i now have is in a module. On form one, during the file copy, I want to include a progress bar that runs during the phase where the files are being copied and directories created. How can I do this? The form is called Form1.

Thanks again

Paul

pauledavey at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

This is an ideal application for the background worker component. I haven't done much work with that, yet, but look it up in the help. You should be able to put the file copying functionality in the background worker (thread) and have it periodically update a progress bar.

SJWhiteley at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9

Ok - so everything copies ok, and I have figured out how to copy the files and remove the read only attribute from them, but I cant remove the readonly attribute from the folders, I can only do this for the files.

Where am I going wrong?

Module mod_filemanagement

' This sub is used to copy all files and subfolders in a folder to a specified destination

' The sub is written in this way, to ensure that we dont hammer the CPU during the process of copying all of the files

Public Sub CopyDirectory(ByVal strsrc As String, ByVal strdest As String)

Dim dirInfoActual As IO.DirectoryInfo = New IO.DirectoryInfo(strsrc)

Dim fsInfo As IO.FileInfo

Dim fsname

Dim dirInfo As IO.DirectoryInfo

For Each dirInfo In dirInfoActual.GetDirectories()

Dim subDirectory As String = IO.Path.Combine(strdest, dirInfo.Name)

IO.Directory.CreateDirectory(subDirectory)

CopyDirectory(dirInfo.FullName, subDirectory)

Next

For Each fsInfo In dirInfoActual.GetFiles()

fsInfo.CopyTo(IO.Path.Combine(strdest, fsInfo.Name), True)

SetAttr(strdest, FileAttribute.Normal) ' this does not work ok

SetAttr(IO.Path.Combine(strdest, fsInfo.Name), FileAttribute.Normal) ' this works ok

'To do: progress bar run here

Next

End Sub

pauledavey at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 10

The process seem to fail when you tried to copy Autorun.inf

I use McAfee for virus protection which (quite annoyingly) forbids the copying of any file called autorun.inf. It produces an "access denied" condition which then sends us scurrying to look for a permission/timing solution. Could that be your issue here?

Ardayon at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...