Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. CopyFile method and String conversions

CopyFile method and String conversions

Scheduled Pinned Locked Moved Managed C++/CLI
c++helptutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paul9038
    wrote on last edited by
    #1

    The following code is from a Visual C++ app to copy a file from one directory to another. It has been simplified, so I can get it working, from the objective where button and textbox controls will populate the String^ variables. I think its pretty clear what the code is trying to do, however I can't get it to work. I would appreciate any help in fixing it, any suggestions of a better approach if this one seems wrong or even some code/advice on how to trap and read any errorcodes that the CopyFile function is trowing instead of the simple messagebox I'm using. Thanks in advance, String^ source = "C:\Temp\Firefox.pcv"; String^ dest = "E:\Test\Firefox.pcv"; char* file1 = (char*)(Marshal::StringToHGlobalAnsi(source + "\0")).ToPointer(); char* file2 = (char*)(Marshal::StringToHGlobalAnsi(dest + "\0")).ToPointer(); if(CopyFile((LPCTSTR)file1, (LPCTSTR)file2, TRUE) == 0){ MessageBox::Show("ERROR"); }else{ MessageBox::Show("OKAY"); } Marshal::FreeHGlobal(IntPtr((void*)file1)); Marshal::FreeHGlobal(IntPtr((void*)file2));

    C M Z 3 Replies Last reply
    0
    • P paul9038

      The following code is from a Visual C++ app to copy a file from one directory to another. It has been simplified, so I can get it working, from the objective where button and textbox controls will populate the String^ variables. I think its pretty clear what the code is trying to do, however I can't get it to work. I would appreciate any help in fixing it, any suggestions of a better approach if this one seems wrong or even some code/advice on how to trap and read any errorcodes that the CopyFile function is trowing instead of the simple messagebox I'm using. Thanks in advance, String^ source = "C:\Temp\Firefox.pcv"; String^ dest = "E:\Test\Firefox.pcv"; char* file1 = (char*)(Marshal::StringToHGlobalAnsi(source + "\0")).ToPointer(); char* file2 = (char*)(Marshal::StringToHGlobalAnsi(dest + "\0")).ToPointer(); if(CopyFile((LPCTSTR)file1, (LPCTSTR)file2, TRUE) == 0){ MessageBox::Show("ERROR"); }else{ MessageBox::Show("OKAY"); } Marshal::FreeHGlobal(IntPtr((void*)file1)); Marshal::FreeHGlobal(IntPtr((void*)file2));

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      Because you are using managed code why don't you just use: File::Copy( path, path2 ); Here is the MSDN article.

      Greetings Covean

      1 Reply Last reply
      0
      • P paul9038

        The following code is from a Visual C++ app to copy a file from one directory to another. It has been simplified, so I can get it working, from the objective where button and textbox controls will populate the String^ variables. I think its pretty clear what the code is trying to do, however I can't get it to work. I would appreciate any help in fixing it, any suggestions of a better approach if this one seems wrong or even some code/advice on how to trap and read any errorcodes that the CopyFile function is trowing instead of the simple messagebox I'm using. Thanks in advance, String^ source = "C:\Temp\Firefox.pcv"; String^ dest = "E:\Test\Firefox.pcv"; char* file1 = (char*)(Marshal::StringToHGlobalAnsi(source + "\0")).ToPointer(); char* file2 = (char*)(Marshal::StringToHGlobalAnsi(dest + "\0")).ToPointer(); if(CopyFile((LPCTSTR)file1, (LPCTSTR)file2, TRUE) == 0){ MessageBox::Show("ERROR"); }else{ MessageBox::Show("OKAY"); } Marshal::FreeHGlobal(IntPtr((void*)file1)); Marshal::FreeHGlobal(IntPtr((void*)file2));

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        "I can't get it to work" isn't real helpful to us. Did you check the error code? Also, for what it's worth, your casts of char* to LPCTSTR in the CopyFile() call are wrong. I always recommend, if you're going to use a cast, ask yourself why you need a cast. Because it won't compile without it? If so, why? Then if you do really need the cast, use the right types.

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • P paul9038

          The following code is from a Visual C++ app to copy a file from one directory to another. It has been simplified, so I can get it working, from the objective where button and textbox controls will populate the String^ variables. I think its pretty clear what the code is trying to do, however I can't get it to work. I would appreciate any help in fixing it, any suggestions of a better approach if this one seems wrong or even some code/advice on how to trap and read any errorcodes that the CopyFile function is trowing instead of the simple messagebox I'm using. Thanks in advance, String^ source = "C:\Temp\Firefox.pcv"; String^ dest = "E:\Test\Firefox.pcv"; char* file1 = (char*)(Marshal::StringToHGlobalAnsi(source + "\0")).ToPointer(); char* file2 = (char*)(Marshal::StringToHGlobalAnsi(dest + "\0")).ToPointer(); if(CopyFile((LPCTSTR)file1, (LPCTSTR)file2, TRUE) == 0){ MessageBox::Show("ERROR"); }else{ MessageBox::Show("OKAY"); } Marshal::FreeHGlobal(IntPtr((void*)file1)); Marshal::FreeHGlobal(IntPtr((void*)file2));

          Z Offline
          Z Offline
          zhushaolin2005
          wrote on last edited by
          #4

          code as following, String^ source = "C:\\Temp\\Firefox.pcv"; String^ dest = "E:\\Test\\Firefox.pcv"; System::IO::File::Copy(source,dest); using the clr base as much as better,it's simple and clear. the system::io::file is a static class .

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups