I have a file names abc.txt on desktop. I wanna copy it to d:\workfile folder.

If I use the code-- File.Copy(strsourceFilePath,strtargetFolderPath,true);

I am getting a exception that Access is Denied.

If I use Move method ,File.Move(strsourceFilePath,strtargetFolderPath);

am getting the exception Cannot create a file , already exists.


Can anybody help me...?

Recommended Answers

All 3 Replies

Sure, you use Windows Vista??
Right click on this program and Run as administrator.

Here's a neat trick I found recently.

The code can easilly be modified to suit your project, enjoy.

public static class CopyUtility
        {
            public static void CopyAllFiles
                (string sourceDirectory, string targetDirectory, bool recursive)
            {
                if (sourceDirectory == null)
                    throw new ArgumentNullException(@"C:/Source");
                if (targetDirectory == null)
                    throw new ArgumentNullException(@"C:/Target");

                CopyAllFiles(new DirectoryInfo(sourceDirectory), new
                    DirectoryInfo(targetDirectory), recursive);
            }

            public static void CopyAllFiles
                (DirectoryInfo source, DirectoryInfo target, bool recursive)
            {
                if (source == null)
                    throw new ArgumentNullException(@"C:/Source");
                if (target == null)
                    throw new ArgumentNullException(@"C:/Target");

                if (!source.Exists)
                    target.Create();

                foreach (FileInfo file in source.GetFiles())
                {
                    file.CopyTo(Path.Combine(target.FullName, file.Name), true);
                }
                if (!recursive) return;
                foreach (DirectoryInfo directory in source.GetDirectories())
                {
                    CopyAllFiles(directory, new DirectoryInfo(Path.Combine
                        (target.FullName, directory.Name)), recursive);
                }
            }
        }

Now simply call this at a click event or whatever CopyUtility.CopyAllFiles(@"C:/Source", @"C:/Target", true); I found this method when googling for Copy functions in C#, and just modified it to suit my application.
Hope this solves your problem.

hi..
Thanks...
I got it. Actually I have to get the name of the file along with the folder name.
thats y it was showing an exception..

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.