Set current EXE to execute file as process working directory

Source: Internet
Author: User

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

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.