[.net] getting full path of my .exe

Started by
8 comments, last by DaWanderer 18 years, 5 months ago
how do i do it? by full path i mean "C:\bleh\hello.exe"
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Advertisement
You can try GetModuleFileName(NULL).
System.Windows.Forms.Application.ExecutablePath
... or System.Reflection.Assembly.GetEntryAssembly().Location :-)
Application.StartupPath
AppDomain.CurrentDomain.BaseDirectory

I don't think any of these (besides the Win32 call) will give you the hello.exe, though, just the path.
This is very OS Specific...

For windows 32:As SiCrane mentioned, you want to use GetModuleFilename to do this. This will give you the desired behavior you are looking for. The syntax for that is:

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


You will want to pass NULL as hModule, that represents the currently running process. make sure lpFilename points to an already created buffer zone. That means if you are creating your data on the heap, be sure to new it beforehand, and delete it afterward. nSize should be the number of bytes that your buffer can hold, it is important that this is set to the size of the buffer (or smaller) or you could easily have yourself a buffer overrun.

Also, for some reason i think there may be an undesired differing behaviour on Win95, though i may be thinking of the TOOLHELP and PSAPI functionality which i find i use quite often (remote process enumeration and such).

Anyways, good luck!
Richard
Quote:Original post by nagromo
AppDomain.CurrentDomain.BaseDirectory

I don't think any of these (besides the Win32 call) will give you the hello.exe, though, just the path.




=/
FileInfo fi = new FileInfo(Application.ExecutablePath);string appPath = fi.DirectoryName;

If you wanted just the directory.

[Edited by - Nypyren on November 10, 2005 2:41:19 AM]
This will get you the full exe path:

string exePath = Environment.GetCommandLineArgs()[0];

This topic is closed to new replies.

Advertisement