Get Application Directory

Best way to get application folder path

AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory.

In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client application, it will be the directory containing the main executable.

In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your application, not, say, the path to the Excel executable.

The others may return different directories depending on your environment - for example see @Vimvq1987's answer.

CodeBase is the place where a file was found and can be a URL beginning with http://. In which case Location will probably be the assembly download cache. CodeBase is not guaranteed to be set for assemblies in the GAC.

UPDATE
These days (.NET Core, .NET Standard 1.3+ or .NET Framework 4.6+) it's better to use AppContext.BaseDirectory rather than AppDomain.CurrentDomain.BaseDirectory. Both are equivalent, but multiple AppDomains are no longer supported.

Get Application Directory


PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;

If eclipse worries about an uncaught NameNotFoundException, you can use:

PackageManager m = getPackageManager();
String s = getPackageName();
try {
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
Log.w("yourtag", "Error Package name not found ", e);
}

Get current folder path

You should not use Directory.GetCurrentDirectory() in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut.

It's better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); for your purpose. This returns the pathname where the currently executing assembly resides.

While my suggested approach allows you to differentiate between the executing assembly, the entry assembly or any other loaded assembly, as Soner Gönül said in his answer,

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

may also be sufficient. This would be equal to

System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

How can I get the application's path in a .NET console application?

System.Reflection.Assembly.GetExecutingAssembly().Location1

Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.

1As per Mr.Mindor's comment:

System.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 'permanent' path of the assembly.

Correct way to get Application directory path in Mac App

You should not be writing anything to the bundle or the folder where the app is located. And if your app is sandboxed, you wouldn't be able to.

Use the "Application Support" folder instead:

NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *applicationSupport = [manager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:false error:&error];
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
NSURL *folder = [applicationSupport URLByAppendingPathComponent:identifier];
[manager createDirectoryAtURL:folder withIntermediateDirectories:true attributes:nil error:&error];
NSURL *fileURL = [folder URLByAppendingPathComponent:@"TSPlogfile.txt"];

I've used NSURL implementation above, but the same concept applies if using paths, too.

See File System Programming Guide: macOS Library Directory Details.

You ask:

Can you explain why the [[NSFileManager defaultManager] currentDirectoryPath] fails while running app from finder?

The folder you happen to be looking at in Finder is not the same thing as the "current directory" when the app is run. In short, the "current directory" has nothing to do with where the app is located.

Get application directory using C# Console Application?

If you still want to use Application.ExecutablePath in console application you need to:

  1. Add a reference to System.Windows.Forms namespace
  2. Add System.Windows.Forms to your usings section

    using System;
    using System.IO;
    using System.Windows.Forms;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
    Console.WriteLine(appDirectory);
    }
    }
    }

Also you can use Directory.GetCurrentDirectory() instead of Path.GetDirectoryName(Application.ExecutablePath) and thus you won't need a reference to System.Windows.Forms.

If you'd like not to include neither System.IO nor System.Windows.Forms namespaces then you should follow Reimeus's answer.

Get Application Directory from within Configure method in Startup.Cs

you can stash the environment in a local property and then you can access it to get the base path like this:

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);


Configuration = builder.Build();

environment = env;
}

public IHostingEnvironment environment { get; set; }
public IConfigurationRoot Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
// you can access environment.ContentRootPath here
}

How to get execution directory of console application

Use Environment.CurrentDirectory.

Gets or sets the fully qualified path of the current working directory.

(MSDN Environment.CurrentDirectory Property)

string logsDirectory = Path.Combine(Environment.CurrentDirectory, "logs");

If your application is running in c:\Foo\Bar logsDirectory will point to c:\Foo\Bar\logs.

How to get application path

The AppDomain.CurrentDomain.BaseDirectory property gets the base directory that the assembly resolver uses to probe for assemblies.

So it's functioning 100% as it should. If you were to build your application, cut and paste it somewhere else in another folder or drive. Those changes would be reflected in this property.

Also, you mentioned that you do not want this part bin\Debug, so you want what's before that? Please be specific.



Related Topics



Leave a reply



Submit