I summarized two methods from the Internet and analyzed them based on my experience. Two global methods:
1. getcurrentdirectory Method
Cstring getcurrworkingdir () </P> <p >{</P> <p> cstring strpath; <br/> getcurrentdirectory (max_path, strpath. getbuffer (max_path); <br/> // The current path returned by the function changes with the operation of the application, for example, when the application is running, the path to the directory where the application is located is obtained. when compiling and running a project, the real host is IDE, so the current directory is the directory where the project is located, not the debug or release directory. note this. When the application has operations such as "Open File", the path obtained by the function is "Open File" at the end of the operation, instead of the directory path where the application is running <br/> strpath. releasebuffer (); <br/> return strpath; <br/>}< br/>
2. getmodulefilename Method
Cstring cpropertysampleapp: getcurrworkingdir () <br/>{< br/> cstring strpath; <br/> tchar szfull [_ max_path]; <br/> tchar szdrive [_ max_drive]; <br/> tchar szdir [_ max_dir]; <br/>:: getmodulefilename (null, szfull, sizeof (szfull) /sizeof (tchar); <br/> _ tsplitpath (szfull, szdrive, szdir, null, null); <br/> _ tcscpy (szfull, szdrive ); <br/> _ tcscat (szfull, szdir); <br/> strpath = cstring (szfull); <br/> return strpath; <br/>}< br/>
This method can obtain the full path of the main module owned by the running process. The first parameter can be used to specify that the main module belongs to another process, if the default value is null, the full path of the executable module of the currently running process is obtained.