C + + Build folder Delete folder get run file path

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.