怎样得到指定文件的路径

yxq123 2007-04-09 11:33:06
我有一个文件main.exe,但是不知道放在哪个盘哪个文件夹中,我想得到这个程序的完全路径,要用什么函数?怎样做?谢谢!
...全文
394 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxq123 2007-04-10
  • 打赏
  • 举报
回复
多谢各位,看来是没有什么好方法了。还是用GetModuleFileName 或者 GetFullPathName吧。
快乐鹦鹉 2007-04-09
  • 打赏
  • 举报
回复
如果程序还没有运行,那么你就做个递归的Find吧。整个机器搜索。。。。。。
WingForce 2007-04-09
  • 打赏
  • 举报
回复
以下例程摘自msdn,打印系统全部进程所有模块的文件路径和名称


#include <windows.h>
#include <stdio.h>
#include "psapi.h"

void PrintModules( DWORD processID )
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;

// Print the process identifier.

printf( "\nProcess ID: %u\n", processID );

// Get a list of all the modules in this process.

hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL == hProcess)
return;

if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
{
char szModName[MAX_PATH];

// Get the full path to the module's file.

if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
sizeof(szModName)))
{
// Print the module name and handle value.

printf("\t%s (0x%08X)\n", szModName, hMods[i] );
}
}
}

CloseHandle( hProcess );
}

int main( )
{
// Get the list of process identifiers.

DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;

if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 1;

// Calculate how many process identifiers were returned.

cProcesses = cbNeeded / sizeof(DWORD);

// Print the name of the modules for each process.

for ( i = 0; i < cProcesses; i++ )
PrintModules( aProcesses[i] );

system( "PAUSE" );

return 0;
}
ouyh12345 2007-04-09
  • 打赏
  • 举报
回复
main.exe内部可以用GetModuleFileName得到路径
kw3465 2007-04-09
  • 打赏
  • 举报
回复
给楼主一点实际,不要空说。谁不会空口白话呢?
dizzo 2007-04-09
  • 打赏
  • 举报
回复
没啥捷径,用BeRoy(失眠)的方法开多线程去Find吧,呵呵
快乐鹦鹉 2007-04-09
  • 打赏
  • 举报
回复
没有现成的函数的。可以参考楼上的代码。呵呵。不过效率不一定高啊。现在的硬盘太大,文件数量太大了。
BeRoy 2007-04-09
  • 打赏
  • 举报
回复
#define _WIN32_WINNT 0x0501

#include <windows.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH]; // directory specification
DWORD dwError;

printf ("Target directory is %s.\n", argv[1]);
strncpy (DirSpec, argv[1], strlen(argv[1])+1);
strncat (DirSpec, "\\*", 3);

hFind = FindFirstFile(DirSpec, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u\n", GetLastError());
return (-1);
}
else
{
printf ("First file name is %s\n", FindFileData.cFileName);
while (FindNextFile(hFind, &FindFileData) != 0)
{
printf ("Next file name is %s\n", FindFileData.cFileName);
}

dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u\n", dwError);
return (-1);
}
}
return (0);
}
//------------------------------------------------
主要是2个函数的使用:
FindFirstFile
FindNextFile
yxq123 2007-04-09
  • 打赏
  • 举报
回复
TO happyparrot(快乐鹦鹉)
你能具体给我几个函数吗?谢谢!那种不管程序有没有运行都能够搜索到的。
yxq123 2007-04-09
  • 打赏
  • 举报
回复
TO ouyh12345(五岭散人)
谢谢你,我要的不是这种。我要的是单独的外挂小程序,指定文件或者程序名称,就能够找到文件路径。



TO WingForce(初六,履霜,坚冰至。)
谢谢你,你的得到文件路径的函数是GetModuleFileNameEx()和GetModuleFileName差不多。

我要的就好是搜索文件一样。

16,471

社区成员

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

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

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