有什么方法可以获取到当前应用程序所在目录? ^_^

Jennifers 2005-09-15 01:22:23
如题...

^_^
...全文
204 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
whs1980 2005-09-16
  • 打赏
  • 举报
回复
_splitpath, _wsplitpath
Break a path name into components.

void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );

void _wsplitpath( const wchar_t *path, wchar_t *drive, wchar_t *dir, wchar_t *fname, wchar_t *ext );

Routine Required Header Compatibility
_splitpath <stdlib.h> Win 95, Win NT
_wsplitpath <stdlib.h> or <wchar.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

None

Parameters

path

Full path

drive

Optional drive letter, followed by a colon (:)

dir

Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used.

fname

Base filename (no extension)

ext

Optional filename extension, including leading period (.)

Remarks

The _splitpath function breaks a path into its four components. _splitpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wsplitpath is a wide-character version of _splitpath; the arguments to _wsplitpath are wide-character strings. These functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tsplitpath _splitpath _splitpath _wsplitpath


Each argument is stored in a buffer; the manifest constants _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, and _MAX_EXT (defined in STDLIB.H) specify the maximum size necessary for each buffer. The other arguments point to buffers used to store the path elements. After a call to _splitpath is executed, these arguments contain empty strings for components not found in path. You can pass a NULL pointer to _splitpath for any component you don’t need.

Example

/* MAKEPATH.C */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];

_makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" );
printf( "Path created with _makepath: %s\n\n", path_buffer );
_splitpath( path_buffer, drive, dir, fname, ext );
printf( "Path extracted with _splitpath:\n" );
printf( " Drive: %s\n", drive );
printf( " Dir: %s\n", dir );
printf( " Filename: %s\n", fname );
printf( " Ext: %s\n", ext );
}


Output

Path created with _makepath: c:\sample\crt\makepath.c

Path extracted with _splitpath:
Drive: c:
Dir: \sample\crt\
Filename: makepath
Ext: .c
jimi5013 2005-09-16
  • 打赏
  • 举报
回复
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left(nPos);

sPath即为去掉.exe的路径,想得到进一步的路径,方法同上
firstofworld 2005-09-16
  • 打赏
  • 举报
回复
TCHAR *path = new char[MAX_PATH+1];
GetModuleFileName(::GetModuleHandle(NULL),path,MAX_PATH);
strProgramPath = path; // 当前的路径名+你的程序名
strProgramPath.TrimRight(str);//str就是你的完整的程序名,例如A.exe,str = "A.exe"
delete [] path;
Joe 2005-09-15
  • 打赏
  • 举报
回复
TCHAR szAppPath[MAX_PATH];
memset(szAppPath, 0, MAX_PATH);
GetModuleFileName(NULL, szAppPath, MAX_PATH);
CString str;
str.Format("%s", szAppPath);
str = str.Left(str.ReverseFind('\\') + 1);
以上只是一种方法,其它的自己再慢慢研究。
pt2519515 2005-09-15
  • 打赏
  • 举报
回复
如何截取相应部分很简单,恐怕各位老鸟不肯解答,我这CSDN新鸟本也不想

态度甚佳,非恶人。


char ch[256];
GetModuleFileName(hModule,ch,255);
for(int i=strlen(ch);i && ch[i]!='\\';i--);
ch[i]=0;

C似易似难人难断
Jennifers 2005-09-15
  • 打赏
  • 举报
回复
*^_^*

我是菜鸟~~~

请问用GetModuleFileName()获取可执行文件的路径后,如何截取相应部分?

请大家帮帮忙... 多谢了!
phoenix96_2000 2005-09-15
  • 打赏
  • 举报
回复
wa, 已经贴了这么了
phoenix96_2000 2005-09-15
  • 打赏
  • 举报
回复
如果是指获取可执行文件的路径,
GetModuleFileName就可以了,然后截取后面的路径就行了

hyamw 2005-09-15
  • 打赏
  • 举报
回复
DWORD GetModuleFileName(
HMODULE hModule, // handle to module to find filename for
LPTSTR lpFilename, // pointer to buffer to receive module path
DWORD nSize // size of buffer, in characters
);

Parameters
hModule
Handle to the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.
lpFilename
Pointer to a buffer that is filled in with the path and filename of the given module.
nSize
Specifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated.
//这个处理一下也可以
flyelf 2005-09-15
  • 打赏
  • 举报
回复
GetModuleFileName
hyamw 2005-09-15
  • 打赏
  • 举报
回复
The GetCurrentDirectory function retrieves the current directory for the current process.

DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // pointer to buffer for current directory
);

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧