Set current EXE to execute file as process working directory
Two ways:
1, API
void _splitpath ( const char *path, char *Drive, char *dir, Char *fname, char *ext );
This function decomposes the full name of the file (with the path) into the path name, filename, and suffix name.
2, API
BOOL Pathremovefilespec (LPTSTR pszpath);
Examples of Use:
< Span style= "font-size:18px" >
#include <windows.h> #include <iostream> #include <shlwapi.h>//pathremovefilespec function header file #pragma Comment (lib, "Shlwapi.lib")//vs2013 need to be added to use Pathremovefilespecusing namespace Std;int main () {char szcurrentdirectory [MAX_PATH], szexefilepathfilename[max_path];//gets the current process working directory GetCurrentDirectory (MAX_PATH, szcurrentdirectory);// Szcurrentdirectory = = Output E:\Projects\1cout << "process Current working directory:" << szcurrentdirectory <<endl;// Szexefilepathfilename = = exe path is E:\Projects\1\Debug\11.exeGetModuleFileName (NULL, Szexefilepathfilename, MAX_PATH) ; Char Drive[max_path], Dir[max_path], Fname[max_path], ext[max_path];//szexefilepathfilename = = exe path E:\Projects\1\ Debug\11.exe//drive = = E: drive letter//dir = = \projects\1\debug\ file in the middle of the path//fname = = 11 filename with extension name//ext = =. exe file extension//The first way to get E Xe file directory _splitpath (szexefilepathfilename, Drive, dir, fname, ext);//combine the disk letter and the file middle path E:\Projects\1\Debug\ strcat (drives, DIR); GetModuleFileName (NULL, Szexefilepathfilename, MAX_PATH);//The second way to get the exe file directory//szexefilEpathfilename = = E:\Projects\1\Debug\11.exe Pathremovefilespec (szexefilepathfilename);// Set process working directory Szexefilepathfilename = = E:\Projects\1\DebugSetCurrentDirectory (szexefilepathfilename);// Get process working directory GetCurrentDirectory (MAX_PATH, szcurrentdirectory);//szcurrentdirectory = = E:\Projects\1\Debugcout << "After the adjustment, the working directory of the process is:" << szcurrentdirectory << endl;system ("pause"); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Set current EXE to execute file as process working directory