获取当前文件的完整路径名的函数?

scorpioding 2004-10-18 05:19:26
在windows VC编程中用哪个函数可以获取当前文件的完整路径名?当然这个路径名的最后要有这个文件名。我的问题的来源是:当我用鼠标双击某个文件的时候,我想得到这个文件所在的完整路径及文件名,请指教,谢谢!!!
...全文
917 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
xdljf 2004-10-18
  • 打赏
  • 举报
回复
shell编程,看看这个
http://www.vckbase.com/document/viewdoc/?id=827
菲斯可儿 2004-10-18
  • 打赏
  • 举报
回复
GetModuleFileName 没错~
fanccYang 2004-10-18
  • 打赏
  • 举报
回复
GetModuleFileName

The GetModuleFileName function retrieves the fully qualified path for the specified module.

To specify the process that contains the module, use the GetModuleFileNameEx function.


DWORD GetModuleFileName(
HMODULE hModule,
LPTSTR lpFilename,
DWORD nSize
);

Parameters
hModule
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module.
lpFilename
[out] Pointer to a buffer that receives a null-terminated string that specifies the fully-qualified path of the module. If the length of the path exceeds the size specified by the nSize parameter, the function succeeds and the string is truncated to nSize characters and null terminated.
The path can have the prefix "\\?\", depending on how the module was loaded. For more information, see Naming a File.

nSize
[in] Size of the lpFilename buffer, in TCHARs.
oyljerry 2004-10-18
  • 打赏
  • 举报
回复
GetFullPathName
Jarrylogin 2004-10-18
  • 打赏
  • 举报
回复
3: 得到当前目录 (GetCurrentDirectory)
char CurPath[MAX_PATH];
DWORD size=MAX_PATH;
GetCurrentDirectory(size,CurPath);
AfxMessageBox(CurPath);

//
CString number;
int len = LineLength(LineIndex(0));
LPTSTR p=number.GetBuffer(len);
this->GetLine(0,p,len);
AfxMessageBox(number);
得到系统目录 (GetSystemDirectory)

mynamelj 2004-10-18
  • 打赏
  • 举报
回复
//--------------------------------------------------------------------//
// Use the code can get file name and path.
// 这些代码是用于获得文件名和路径.
// 多功能文件对话框
//--------------------------------------------------------------------//

OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HANDLE hf; // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = GetSafeHwnd(); // To the parent window handle.
ofn.lpstrFile = szFile;
//
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box.

if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
Wenxy1 2004-10-18
  • 打赏
  • 举报
回复
The GetFullPathName function retrieves the full path and file name of the specified file.


DWORD GetFullPathName(
LPCTSTR lpFileName,
DWORD nBufferLength,
LPTSTR lpBuffer,
LPTSTR* lpFilePart
);

Parameters
lpFileName
[in] Pointer to a null-terminated string that specifies a valid file name. This string can use either short (the 8.3 form) or long file names.
nBufferLength
[in] Size of the buffer to receive the null-terminated string for the drive and path, in TCHARs.
lpBuffer
[out] Pointer to a buffer that receives the null-terminated string for the drive and path.
lpFilePart
[out] Pointer to a buffer that receives the address (in lpBuffer) of the final file name component in the path.
Return Values
If the function succeeds, the return value is the length of the string copied to lpBuffer, not including the terminating null character, in TCHARs.

If the lpBuffer buffer is too small to contain the path, the return value is the size of the buffer required to hold the path plus the terminating null character, in TCHARs. Therefore, if the return value is greater than nBufferLength, call the function again with a buffer that is large enough to hold the path.

If the function fails for any other reason, the return value is zero. To get extended error information, call GetLastError.
elssann 2004-10-18
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
printf("The full path of the executing program is : %Fs\n",
_pgmptr);
}


_pgmptr就是了

如果你要分解路径,可以用_splitpath对 _pgmptr进行分解,能满足你的任何需要
可以在MSDN里查到使用方法
Visual_Li 2004-10-18
  • 打赏
  • 举报
回复
所以应该是GetModuleFileName
Visual_Li 2004-10-18
  • 打赏
  • 举报
回复
楼主的意思我想可能不一定是当前目录下的文件了
ustbzhangwei 2004-10-18
  • 打赏
  • 举报
回复
CString strFileName;

CFileDialog dlg(TRUE);
if(dlg.DoModal() == IDOK)
{
strFileName = dlg.GetPathName();
MessageBox(strFileName);
}
huwei001982 2004-10-18
  • 打赏
  • 举报
回复
楼主的意思可能是....


GetModuleFileName 得到的 PATH
Daniel22_cn 2004-10-18
  • 打赏
  • 举报
回复
GetModuleFileName
DentistryDoctor 2004-10-18
  • 打赏
  • 举报
回复
GetFullPathName与当前目录有关,其它目录的可能不能正确获取,它需要你自己通过其它方法得到。
kugou123 2004-10-18
  • 打赏
  • 举报
回复
GetFullPathName
iamknight 2004-10-18
  • 打赏
  • 举报
回复
当前文件是一个相对的概念吧,没太明白你的意思。
  • 打赏
  • 举报
回复
http://www.vcshare.net/bbs/ShowPost.asp?id=1320

不明白你的意思
是不是?
DentistryDoctor 2004-10-18
  • 打赏
  • 举报
回复
GetFullPathName

16,550

社区成员

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

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

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