Andri Gunawan's Blog

.NET Developer
See also: Other Geeks@INDC

August 2006 - Posts

How to get the application directory?

Kebetulan ngeliat di codeguru forum di link ini http://www.codeguru.com/forum/showthread.php?t=312471 .Jadi lumayan untuk referensi.

// With STL string
#include <string>

char        szAppPath[MAX_PATH] = "";
std::string strAppDirectory;

::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);

// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));



// With CString
char    szAppPath[MAX_PATH] = "";
CString strAppDirectory;

::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);

// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));



// With standard string
char szAppPath[MAX_PATH]      = "";
char szAppDirectory[MAX_PATH] = "";

::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);

// Extract directory
strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, '\\') - szAppPath);
szAppDirectory[strlen(szAppDirectory)] = '\0';

 

Share this post: | | | |
Posted: Aug 02 2006, 03:25 AM by andri | with no comments
Filed under: