MFC detects a file exists, gets the current path, creates a new folder

Source: Internet
Author: User

Directly on the code:

See if there is a DB folder under the path to the execution file, and if not, create a new/////////

CFileFind CFF;
if (CFF. FindFile (_t ("db")) = = 0)//current path, DB folder not found
{
CString StrText;
TCHAR exepath[max_path]={0};
:: GetModuleFileName (Null,exepath,max_path); Get exe path, stored in ExePath
Strtext=exepath;
StrText = Strtext.left (strtext.reversefind (' \ \ ')); Find the first/last number in the path, and then pinch off the characters that follow it, paying attention to the expression of the escape character \ \
StrText + = _t ("\ \");
StrText + = _t ("db"); New path, new file name
CreateDirectory (Strtext,null); Generate DB Folder
}

The following is reproduced, not verified

1. Get the path where debug or release is located
CString Getmoduledir ()
{
hmodule module = getmodulehandle (0);
Char Pfilename[max_path];
GetModuleFileName (module, Pfilename, MAX_PATH);

CString Csfullpath (pfilename);
int nPos = Csfullpath.reversefind (_t (' \ \ '));
if (NPos < 0)
Return CString ("");
Else
Return Csfullpath.left (NPos);
}

2. Get the current working path (the path where the DSP is located)

Get Work Path
CString Getworkdir ()
{
Char Pfilename[max_path];
int nPos = GetCurrentDirectory (MAX_PATH, pfilename);

CString Csfullpath (pfilename);
if (NPos < 0)
Return CString ("");
Else
return csfullpath;
}

The following is reproduced, not verified

C/mfc How to get the application's current path (grooming)

The first method:DWORD GetCurrentDirectory (
DWORD nbufferlength,//size, in characters, of directory buffer
LPTSTR lpbuffer//pointer to buffer for current directory
);
BOOL SetCurrentDirectory (
LPCTSTR lppathname//Pointer to name of the new current directory
);
The second method ofUse GetModuleFileName to get the file name of the application (the first parameter is null)
Then use _splitpath to parse the file name to get the path for example://Get current path
/*char buf[100];
GetCurrentDirectory (sizeof (BUF), buf);
MessageBox (BUF);
HINSTANCE Hinst=null;
Hinst=afxgetapp ()->m_hinstance;
Char Path_buffer[_max_path];
GetModuleFileName (hinst,path_buffer,sizeof (Path_buffer));//Get the full path of EXE file
Detaches the path and file name.
Char drive[_max_drive];
Char Dir[_max_dir];
Char Fname[_max_fname];
Char Ext[_max_ext];
_splitpath (Path_buffer, Drive, dir, fname, ext);
CString Path;
Path.format ("%s%s", Drive,dir); char path[300];strcpy (path,drive);
strcat (Path,dir); or:TCHAR Exefullpath[max_path];
CString strpath;
GetModuleFileName (Null,exefullpath,max_path);
Strpath= (CString) Exefullpath;
intPosition=strpath.reversefind (' \ \ ');
Strpath=strpath.left (position+1);

TCHAR Filepath[max_path];
GetModuleFileName (Null,filepath,max_path);
(_TCSRCHR (FilePath, ' \ \ ')) [1] = 0;
Lstrcat (filepath,_t ("My.ini"));

The third method:VC __argv[0] can get EXE program name, and then use _splitpath can be decomposed to get the program path. Fourth Method#include <direct.h>char buf[_max_path];_getcwd (Buf,_max_path); the fourth is to get the directory where the operating system is located char buf[100];
GetSystemDirectory (buf,100);
MessageBox (BUF);the following is not verifiedHttp://www.cnblogs.com/project/archive/2010/12/02/1894494.html

MFC detects a file exists, gets the current path, creates a new folder

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.