how can I get my .exe path because if I copy my .exe I can get my new path ?
Asked
Active
Viewed 4.3e+01k times
4 Answers
332
System.Reflection.Assembly.GetEntryAssembly().Location;

Gabriel McAdams
- 56,921
- 12
- 61
- 77
-
167I only wanted the Path but I see this command gives me path + filename of the exe. :-( On the other Hand GetEntryAssembly().Location gives the path with "file://" - What I needed was `AppDomain.CurrentDomain.BaseDirectory` – user799821 Feb 27 '12 at 07:51
-
22This does not work in a Unit Test project. GetEntryAssembly() is null. – Eric J. Apr 26 '13 at 21:43
-
13System.Reflection.Assembly.GetExecutingAssembly().Location returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. System.Reflection.Assembly.GetExecutingAssembly().CodeBase will return the 'permenant' path of the assembly. – Cyrus Sep 26 '14 at 19:20
-
@user799821 You can use Directory.GetCurrentDirectory() to get the current working directory. – Christian Klemm Jun 19 '16 at 16:28
-
`.CodeBase` is better than `.Location`. `.Location` will take you to the .NET Temporary files directory where your files are copied, if you are using a web application. If you want the actual location where the files are stored, `.CodeBase` is the better option, and what I, and probably the OP was actually looking for. @Cyrus is right, and I don't think this should've gotten so many upvotes. – vapcguy Jun 27 '16 at 17:15
-
10@user799821 Just use `Path.GetDirectoryName` to get the folder without the filename. – ProfK Apr 03 '18 at 05:49
-
12@ProfK: thanks, that was exactly what I needed. Full solution worked for me: `System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)` – SnookerC Jul 10 '18 at 20:53
-
This gives you a path with filename, but this: `new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName` gives you path! – Ivan P. Apr 19 '19 at 08:09
-
`CodeBase` has "file:" prepended to the actual location tho. Just a warning. – Seth Nov 17 '22 at 03:28
175
In addition:
AppDomain.CurrentDomain.BaseDirectory
Assembly.GetEntryAssembly().Location
The first, is the directory of the executable of your application. Beware! It can be changed at runtime.
The second, will be the directory of the assembly (.dll) you run the code from.
-
31
-
5`Assembly.GetEntryAssembly().Location` works for me. The first one has `file;\ ` while the second one provides the absolute file path of the executing .exe. Add `System.IO.Path.GetDirectoryName()` to get the path only – CraftedGaming Nov 04 '18 at 14:35
79
In a Windows Forms project:
For the full path (filename included): string exePath = Application.ExecutablePath;
For the path only: string appPath = Application.StartupPath;

Richard Marskell - Drackir
- 13,080
- 3
- 33
- 54
-
13"Application.StartupPath will be effected by "Working Directory" if it's set in the exe shortcut, or the process is started from another apps." – Pedro77 Jan 16 '15 at 10:44
4
in visualstudio 2008 you could use this code :
var _assembly = System.Reflection.Assembly
.GetExecutingAssembly().GetName().CodeBase;
var _path = System.IO.Path.GetDirectoryName(_assembly) ;

Iraj
- 1,492
- 5
- 18
- 42
-
4That's not really correct answer. It will give you a string like "file://foo" – dyatchenko Dec 04 '15 at 23:43
-
Also it will end up with ArgumentException in `System.IO.Path.GetDirectoryName` – benderto Sep 15 '16 at 06:20