Recently to use MFC to develop an interface, there is a requirement is to generate a executable (EXE file) with the same name of the folder, such as my executable file path is D:\KFS\codes\22222\Release\22222.exe, I need to D:\KFS\ The codes\22222\release path generates a 22222 folder to put things. After reviewing a little information, I succeeded. Then wrote a small demo. The following code:
#include <windows.h> #include <iostream> #include <io.h>using namespace Std;int main () {char chpath[ MAX_PATH]; GetModuleFileName (NULL, (LPSTR) chpath,sizeof (Chpath)); Get exe file path cout<<chpath<<endl; int T;for (t=0;t<=sizeof (Chpath); t++) {if (chpath[t]== '. ') {break;}} Char*create,*remove;create=new char[t+4];remove=new char[t+4];create[0]= ' m '; create[1]= ' d '; create[2]= '; Remove[0] = ' R '; remove[1]= ' d '; remove[2]= ';cout<< "t =" <<t<<endl;cout<<sizeof (chpath) <<endl; int I, j;for (i=0,j=3;i<=t;i++,j++) {cout<<chpath[i];if (chpath[i]== '. ') {break;} create[j]= chpath[i];remove[j]= chpath[i];} create[t+3]= ' + '; remove[t+3]= ' ;cout<<endl;cout<<create<<endl;cout<< ' remove "<<remove<<endl;if (_access (chpath,0)!=-1) system (remove);//Delete folder system (create);//Creating Folder System (" Pause "); return 0;}
The code is simple, and the functions that need attention are commented. Another thing is that in VS2010, after a new project, a compilation error occurred
Error C2664: "Getmodulefilenamew": Cannot convert parameter 2 from "LPSTR" to "LPWSTR" regardless of the type that is pointed to, conversion requires reinterpret_cast, C-style conversion, or function style conversion
is because the default is caused by the use of the Unicode character set. The solution is: right---"attribute--" configuration number--"general-" character Set--"Select not set--" OK.
Result diagram:
C + + Build folder Delete folder get run file path