I wrote a recursive function using System.IO stuff in order to copy a directory and all its contents to another location. I'm using Windows 7 and Visual C# 2010.
It looked like a simple task but it really isn't. First, when i tried to run the code i got a permission error. Much research told me that to fix this i had to change the manifest to request better permissions, so i set it to ask for highestAvailable thinking this would surely work.
I was wrong. Next I got an error about my program being a clickonce application that shouldn't recieve rights. Told the IDE that this wasn't a clickonce app.
This caused the program to run but and an UnauthorisedAccessException was thrown by System.IO.File.Copy. I restarted the IDE, this time opening it as Administrator. Same thing happened.
The next thing i tried was setting uiAccess to true in the manifest. Now when i try to build i get a cryptic error message with a broken help link. It says running an Accessability application requires following the steps shown in help (the broken link)
I refuse to believe that simply copying a file should be this difficult. Has anybody managed? You will earn serious hero points if you can tell me how
the function is below. There's some stuff missing but it should be simple to follow my logic:
void copyContents(string source,string dest)
Console.WriteLine("source located. copy commenced");
//now we copy each file inside
string files = System.IO.Directory.GetFiles(source);
foreach (string s in files)
filename = System.IO.Path.GetFileName(s);
dest = System.IO.Path.Combine(dest, filename);
System.IO.File.Copy(source, dest, true); //strange error here
//next we copy each folder inside
string dirs = System.IO.Directory.GetDirectories(source);
File.Copy(source, dest, true) attempts to overwrite files that already exist (that's what the true part means). It will fail if you can't overwrite the file (it's set to read only, for example). FileInfo.CopyTo(dest) does not overwrite files, so it doesn't generate the same errors.
You could change the first to File.Copy(source, dest) and it would act the same as the previous FileInto.CopyTo, or you could change the FileInfo to FileInfo.CopyTo(dest, true) and it would act the same as the first File.Copy.