How do you copy a folder and paste it in a directory in C++/CLI?

bobuck man 61 Reputation points
2021-10-03T13:37:15.01+00:00

How do you copy a folder and paste it in a directory?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,634 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2021-10-04T19:43:19.133+00:00

    To reuse a feature that is already developed in .NET Framework, use the “Add Reference…” command from PROJECT menu and add a reference to Microsoft.VisualBasic assembly. Then try a code like this:

    using namespace Microsoft::VisualBasic::FileIO;
    FileSystem::CopyDirectory( source_folder, destination_folder, UIOption::AllDialogs );
    

    Prepare the corresponding source_folder and destination_folder strings.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 83,201 Reputation points
    2021-10-03T14:09:07.263+00:00

    There are several ways with System::IO

    Another way with the Shell :

    String^ sFrom = "E:\\Test2\\Temp";
    String^ sTo = "E:\\test";
    marshal_context^ context = gcnew marshal_context();
    PCZZTSTR pFrom = context->marshal_as<PCZZTSTR>(sFrom + "\0");
    PCZZTSTR pTo = context->marshal_as<PCZZTSTR>(sTo + "\0");
    auto fos = new SHFILEOPSTRUCT();
    fos->wFunc = FO_COPY;
    fos->pFrom = pFrom;
    fos->pTo = pTo;
    fos->fFlags = FOF_ALLOWUNDO;
    int nReturn = SHFileOperation(fos);
    if (nReturn != 0)
    {
     throw gcnew Win32Exception(nReturn);
    }
    delete context;
    

    At beginning :

    #include <msclr\marshal_cppstd.h>
    using namespace msclr::interop;
    
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <shellapi.h>
    #pragma comment (lib, "Shell32")