[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Copy file method do not create folder???

Posted on 2007-10-14
2
Medium Priority
?
919 Views
Last Modified: 2013-11-26
String ^source = "C:/a.txt";
String ^destination = "C:/folder/a.txt";
try
   {
      // 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????
0
Comment
Question by:xiaoxin85
2 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 20076083
               Directory.CreateDirectory(path)



http://msdn2.microsoft.com/en-us/library/system.io.directory.aspx includes an example (also uses .Exists to make sure it doesn't exist before creating it)
0
 
LVL 11

Expert Comment

by:udhayakumard
ID: 20076231
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


               File::Exists


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))
                    Directory::CreateDirectory(finfo->Directory);


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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

830 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