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)
        {
            if (Directory.Exists(source))
            {
                Console.WriteLine("source located. copy commenced");
                if (!Directory.Exists(dest))
                    System.IO.Directory.CreateDirectory(dest);

                //now we copy each file inside
                string[] files = System.IO.Directory.GetFiles(source);
                string filename;
                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);
                //to do
            }
            else
                Console.WriteLine("invalid Sourcefile");
        }

Recommended Answers

All 5 Replies

Check this code

void copyContents(string source, string dest)
        {
            if (Directory.Exists(source))
            {                
                try
                {
                    MessageBox.Show("source located. copy commenced");
                    if (!Directory.Exists(dest))
                        System.IO.Directory.CreateDirectory(dest);
                    //now we copy each file inside
                    DirectoryInfo di = new DirectoryInfo(source);
                    foreach (FileInfo fi in di.GetFiles())
                    {
                        dest = System.IO.Path.Combine(dest, fi.Name);
                        fi.CopyTo(dest);
                    }
                    //next we copy each folder inside
                    string[] dirs = System.IO.Directory.GetDirectories(source);
                    //to do
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString() + "   " + ee.Source, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
               MessageBox.Show("invalid Sourcefile");           
               }

You Rock my world abelLazm.

Any idea why your code doesn't get permission complaints and mine does though? I may need uiAccess set to true for other reasons...

It feels very nice when your post is helpfull :)....
I don't know exactly but as far as I know

System.IO.File.Copy(source, dest, true);

funtion doesn't allow overwriting of files and

fi.CopyTo(dest);

allows it may be possible that your destination folder contains any of files with same name as in source folder...

If your problem is solved then mark this thread solved :)

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.

So if the file already exists, this will still not copy? do you have to delete the file first then? I have the same issue and am trying to copy a stinking file and it's so very difficult.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.