Copy file method in C++.NET 2005

I want to do s simple copy file from 1 directory to another directory. how to I do  that????  I am using C++.NET 2005 version.
xiaoxin85Asked:
Who is Participating?
 
slado2Commented:
Use File.Copy method from System.IO namespace:

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   String^ path2 = String::Concat( path, "temp" );
   try
   {
     
      // 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 fail.
      File::Copy( path, path2 );
      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 );
   }

}

http://msdn2.microsoft.com/en-us/library/c6cfw35a.aspx
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
An alternative would be to use the SHFileOperation - see in help for more details/example code.
0
 
slado2Commented:
SHFileOperation just brings more overhead - internally after many checks it just calls CopyFileEx
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
AndyAinscowFreelance programmer / ConsultantCommented:
>>>internally after many checks it just calls CopyFileEx

so less for you to code (and make mistakes with or forget to check for) ?
0
 
slado2Commented:
I don't think that CopyFileEx or File.Copy (in .NET) is a complicated method.
File.Copy is guaranteed to be platform independent while SHFileOperaion is not.
0
 
xiaoxin85Author Commented:
So which is way is a better way for it??? File.Copy or SHFileOperaion???
0
 
slado2Commented:
File.Copy
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
SHFileOperation is an alternative - your choice.

ps. Minimum operating system is Win95 or Win NT 4  (Does anyone still use windows 3 ?)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.