Copy file method do not create folder???

Posted on 2007-10-14
Last Modified: 2013-11-26
String ^source = "C:/a.txt";
String ^destination = "C:/folder/a.txt";
      // Ensure that the target does not exist.
      File::Delete( destination );
      // Copy the file.
      File::Copy( source, destination );
      Console::WriteLine( "{0} copied to {1}", source, destination );
      // Try to copy the same file again, which should fail.
      File::Copy( source, destination );
      Console::WriteLine( "The second Copy operation succeeded, which was not expected." );
   catch ( Exception^ e )
      Console::WriteLine( "Double copying is not allowed, as expected." );
      Console::WriteLine( e );

The above code when I tried do not work becoz I had it to copy to a destination folder where it does not exist, how to create I automatic create the copy for it to copy to????
Question by:xiaoxin85
    LVL 37

    Accepted Solution

                   Directory.CreateDirectory(path) includes an example (also uses .Exists to make sure it doesn't exist before creating it)
    LVL 11

    Expert Comment

    And moreover the second copy will also be succeeded bcoz you are not moving the file, just copying the file... Before deleting a file check whether the file exists else it will throw an exception


    To make the directory creation common, since destination path may change use the following

                   String ^destination = "C:/folder/a.txt";

                   FileInfo^ finfo = gcnew FileInfo("C:/folder/a.txt");

                    if (!Directory::Exists(finfo->Directory))

    Sorry if any mistake in my code... I am a C# developer :(


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now