• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

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.
0
xiaoxin85
Asked:
xiaoxin85
  • 4
  • 3
1 Solution
 
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
 
AndyAinscowCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
AndyAinscowCommented:
>>>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
 
AndyAinscowCommented:
SHFileOperation is an alternative - your choice.

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now