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 filesPublicSub CopyDirectory(ByVal strsrcAsString,ByVal strdestAsString)Dim dirInfoActualAs IO.DirectoryInfo =New IO.DirectoryInfo(strsrc)Dim fsInfoAs IO.FileInfoDim fsnameDim dirInfoAs IO.DirectoryInfoForEach 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 hereForEach dirInfoIn dirInfoActual.GetDirectories()Dim subDirectoryAsString = IO.Path.Combine(strdest, dirInfo.Name)IO.Directory.CreateDirectory(subDirectory)
CopyDirectory(dirInfo.FullName, subDirectory)
NextNextEndSub
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 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
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
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 hereNextFor Each dirInfo In dirInfoActual.GetDirectories()Dim subDirectory As String = IO.Path.Combine(strdest, dirInfo.Name)IO.Directory.CreateDirectory(subDirectory)
CopyDirectory(dirInfo.FullName, subDirectory)
'NextNextThanks 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
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 filesPublic Sub CopyDirectory(ByVal strsrc As String, ByVal strdest As String)Dim dirInfoActual As IO.DirectoryInfo = New IO.DirectoryInfo(strsrc)Dim fsInfo As IO.FileInfoDim fsnameDim dirInfo As IO.DirectoryInfoFor Each dirInfo In dirInfoActual.GetDirectories()Dim subDirectory As String = IO.Path.Combine(strdest, dirInfo.Name)IO.Directory.CreateDirectory(subDirectory)
CopyDirectory(dirInfo.FullName, subDirectory)
NextFor 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 hereNextEnd Sub