1

I'm trying to get the directory path to the Debug folder that my application is executing in, or should I say the directory path to where my currently running EXE program is running.

Now, many of you might say to do something like this

System.Reflection.Assembly.GetExecutingAssembly().Location

I can't do this. I need to to get from an alternative assembly. I don't know what assembly this will be in, so I guess it has to work globally without changing it depending on the assembly it's currently in.

I'm looking for the best way (speed & performance thought about).

serialize
  • 33
  • 1
  • 2
  • Use the assembly's Location property? I really don't understand what you're trying to accomplish. Are you trying to get the path to the directory that the application is executing in or the path to the directory where some arbitrary assembly lives? – itsme86 May 31 '18 at 16:20
  • Possible duplicate of [Finding current directory during Visual Studio debugging session?](https://stackoverflow.com/questions/1367732/finding-current-directory-during-visual-studio-debugging-session) – aloisdg May 31 '18 at 16:22
  • I thought my question made it clear, sorry. I'm trying to get the directory path of where the EXE is currently executing. I can't use `System.Reflection.Assembly.GetExecutingAssembly().Location` because I'm not inside the main executing assembly. – serialize May 31 '18 at 16:22
  • @aloisdg I don't understand why you think that's a duplicate? – serialize May 31 '18 at 16:23
  • You can do that by getting parent directory debug...so on of and then navigate through the folders – Mahesh May 31 '18 at 16:25
  • @serialize, Does the following answer answer your concern ? If so, please do accept it as the answer. – S.N Aug 16 '20 at 21:38

1 Answers1

5

System.AppContext.BaseDirectory

This is the prefered replacement for AppDomain.CurrentDomain.BaseDirectory in .net core (at least until the API appears for AppDomain, if it ever will).

AppDomain.CurrentDomain.BaseDirectory

This is the best option all round. It will give you the base directory for class libraries, including those in ASP.NET applications.

Directory.GetCurrentDirectory()

This does an interop call using the winapi GetCurrentDirectory call inside kernel32.dll, which means the launching process’ folder will often be returned. Also as the MSDN documents say, it’s not guaranteed to work on mobile devices.

Environment.CurrentDirectory

This simply calls Directory.GetCurrentDirectory()

Assembly.Location

This would be called using

this.GetType().Assembly.Location

This returns the full path to the calling assembly, including the assembly name itself. If you are calling a separate class library, then its base directory will be returned, such “C:\myassembly.dll” - depending obviously on which Assembly instance is being used.

Application.StartupPath

This is inside the System.Windows.Forms namespace, so is typically used in window forms application only.

Application.ExecutablePath

The same as Application.StartupPath, however this also includes the application name, such as “myapp.exe”

source by chris-s

aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • this.GetType().Assembly.Location gives me '' and both BaseDirectories and CurrentDirectory are giving me '/'. Do none of these work for blazor webassembly websites? – DotNet Programmer Jun 07 '21 at 18:23