954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

can i get the full file path from specified file name (.exe)

pleeeeeease
can i get the full file path from specified file name (.exe)
an arbitrary file (.exe)

lahom
Light Poster
42 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

look at argv[0] -- some compilers put the full path in there.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

...but that is not guaranteed. It is usually a safe bet that it contains at least the simple name of the executable ("foo.exe"). It may also contain either an absolute path or a relative path.

On POSIX systems (Unix, Linux, Mac OS X, etc), use getcwd () and combine the result with argv[0].

On Win32, use GetModuleFileName ().

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

>>but that is not guaranteed
Yes I know, which is why I made the qualified statement. Only some compilers do that.

getcwd() only gets the current working directory, which is also available in MS-Windows. If the program changes directories before calling getcwd() itt will not give you what you are looking for, assuming you want the path to the *.exe file.

I don't know if there is a POSIX equivalent to GetModueFileName(), but getcwd() is not it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

> Yes I know...
I know you know... But I thought I'd give the OP more. ;)

There is no POSIX equivalent to GetModuleFileName(). You have to use getcwd() as soon as your program starts or at least before anything changes the current directory.

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You