Question

This is a code sample from Microsoft, with a different file location, that just won't work:

 string fileName = "test1.txt";
 string sourcePath = @"C:\";
 string targetPath = @"C:\Test\";

 // Use Path class to manipulate file and directory paths.
 string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
 string destFile = System.IO.Path.Combine(targetPath, fileName);

 System.IO.File.Copy(sourceFile, destFile, true);

It can't find the source file. If I set a break point, this is what I get:

    args    {string[0]} string[]
    fileName    "test1.txt" string
    sourcePath  "C:\\"  string
    targetPath  "C:\\Test\\"    string
    sourceFile  "C:\\test1.txt" string
    destFile    "C:\\Test\\test1.txt"   string

So it looks like it is doubling the backslashes even though a verbatim string is used. (there is no doubt that I have a test1.txt file in C:) Any ideas? Thanks!

Was it helpful?

Solution

doubling of backslashes is correct, I think your problem is file name. try to read the file without that operations, but before see if you "Hide Extenstions for known Type" if you do than filename should be test1.txt.txt :)

OTHER TIPS

There are 3 common failure modes:

  1. The source file C:\test1.txt does not exist.
  2. The destination file C:\Test\test1.txt exists but is read-only.
  3. The destination directory C:\Test does not exist.

My guess is that item 3 is the problem and if so you need to make sure that the destination directory exists before you call File.Copy. If this is the case you will be seeing a DirectoryNotFoundException. You can use Directory.CreateDirectory to make sure the destination directory exists.

The double backslash is the normal way to represent a backslash in a string. When you use @ you are saying that you don't want to interpret any escape sequence (among other things, see here for more info, under "Literals")

So the problem is different. Do C:\test1.txt and C:\Test exist? Do you have permission to write in targetPath?

Try the following (add exception handling and more error checking as needed)

if (!Directory.Exists(targetPath)) {
    Directory.CreateDirectory(targetPath);
}
if (File.Exists(sourceFile)) {
    File.Copy(sourceFile,destFile,true);
}

If you are having trouble try and look at this example:

using System;
using System.IO;
class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = path + "temp";

        try 
        {
            // Create the file and clean up handles.
            using (FileStream fs = File.Create(path)) {}

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} copied to {1}", path, path2);

            // Try to copy the same file again, which should succeed.
            File.Copy(path, path2, true);
            Console.WriteLine("The second Copy operation succeeded, which was expected.");
        } 

        catch 
        {
            Console.WriteLine("Double copy is not allowed, which was not expected.");
        }
    }
}

Taken from : http://msdn.microsoft.com/en-us/library/system.io.file.copy(v=vs.71).aspx

To see exactly what goes wrong, you could wrap the code in a try-catch block:

try { // Code that can throw an exception }
catch (Exception ex)
{
    // Show what went wrong
    // Use Console.Write() if you are using a console
    MessageBox.Show(ex.Message, "Error!");
}

The most likely problems are a missing source file, destination folder doesn't exist, or you don't have permission to access that location.

On windows 7 I noticed that you need administrator privileges to write directly to the root of the drive where the operating system is installed (usually c:\). Trying to write a file, or create a directory in that location will probably fail, so I suggest you use another location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top