CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: File Copy Question - Windows Form

  1. #1
    Join Date
    Aug 2016
    Location
    S.E. Michigan
    Posts
    7

    Question File Copy Question - Windows Form

    In a Windows Form application which Method, Function or System command is the most efficient for copying files?

    I want to simply copy files from a parent directory to a sub directory.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,255

    Re: File Copy Question - Windows Form

    The easiest is probably to use CopyFile(). See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.3)

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: File Copy Question - Windows Form

    There's a better alternative without the need to directly call Windows API functions: System::IO::File::Copy(). Other methods of the File class and classes in the System::IO namespace are also useful in this context.
    Last edited by Eri523; October 21st, 2016 at 06:13 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,357

    Re: File Copy Question - Windows Form

    Another option is to use Microsoft::VisualBasic::FileIO::FileSystem::CopyDirectory to copy all the files in the directory in a single shot.

  5. #5
    Join Date
    Aug 2016
    Location
    S.E. Michigan
    Posts
    7

    Re: File Copy Question - Windows Form

    Quote Originally Posted by Arjay View Post
    Another option is to use Microsoft::VisualBasic::FileIO::FileSystem::CopyDirectory to copy all the files in the directory in a single shot.
    It will be a selective copy not a whole directory.

    Newbie question? Can I mix/use CLI API C++ and VD classes, methods, and functions in the same *.h file? I,m using VS Exp 2015

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: File Copy Question - Windows Form

    Quote Originally Posted by OldPCGuy View Post
    Newbie question? Can I mix/use CLI API C++ and VD classes, methods, and functions in the same *.h file? I,m using VS Exp 2015
    Basically: Yes, of course, as long as you don't infringe any rules imposed by the language used or the framowork/OS. However, mostly from a stylistic point of view, but also for the sake of efficiency, it's best to combine as few as possible (or rather, reasonable) libraries/frameworks/components to achieve your goal. Just like the scenario of your question here: Prefer the functionality readily provided by the .NET framework over directly calling Windows APIs.

    A special caution is to be advised regarding the STL/CLR library (cliext namespace). It looks like a pretty convenient and handy tool for new C++/CLI programmers who already are used to native C++, but, due to issues related to its, err, mediocre integration into the .NET framework, it may cause a lot of unnecessary trouble. (Doing a thread title search for the error code LNK2022 can take you to an old thread of mine, describing a scenario where the STL/CLR library really drove me to the edge of madness.) The STL/CLR library really should not be used as a production tool, just for quick and dirty (and transitional!) migration of pre-existing native C++ code to C++/CLI.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7

    Re: File Copy Question - Windows Form

    The CopyFileEx function provides two additional capabilities. CopyFileEx can call a specified callback function each time a portion of the copy operation is completed, and CopyFileEx can be canceled during the copy operation.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: File Copy Question - Windows Form

    Right, these are two points pro CopyFileEx(). But handling native callbacks in a C++/CLI application can be be some expert matter of its own. However, while this particular callback isn't quite trivial, it could be worse: For instance, no structs (or rather, struct pointers) are passed as parameters to the callback.

    For one who is able to properly implement the callback, it shouldn't be too tricky to implement a purely .NET-based file copy with progress reporting either. Perhaps, after all, that would be less hassle than the native callback approach.

    I have written some progress reporting .NET file processing facilities myself over time. None of them is a copy operation, but copying a file is one of the most trivial ways of processing it, so adapting one of these existing implementations to mere copying shouldn't be too complicated.

    Actually, for one application I implemented sort of a progress reporting stream reader (which is even thread-safe), and that's much more than just half the way to a progress reporting copy. While we're at it, I really love the Progress<T> class! It makes progress reporting almost ridiculously simple, in particular handling all that inter-thread marshaling under the hood for you, without any effort of your own. (And I didn't even use that for the aforementioned progress reporting stream reader; I was targeting .NET 4.0 back then, which doesn't feature Progress<T>.)

    Admittedly, the cancelation flag feature of CopyFileEx() is temptingly simple to use, but I think, at least from a user's perspective, a cancelation feature without progress reporting is much less than half the fun anyway...
    Last edited by Eri523; January 24th, 2017 at 04:08 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)