CopyFile method and String conversions
-
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));
-
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));
Because you are using managed code why don't you just use: File::Copy( path, path2 ); Here is the MSDN article.
Greetings Covean
-
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));
"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:
-
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));
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 .