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.
How can I get the application's path in a .NET console application?
System.Reflection.Assembly.GetExecutingAssembly()
.Location
1
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.
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);
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.
What is the best way to determine application root directory?
AppDomain.CurrentDomain.BaseDirectory
is my go to way of doing so.
However:
Application.StartupPath
gets the directory of your executable
AppDomain.BaseDirectory
gets the directory used to resolve assemblies
Since they can be different, perhaps you want to use Application.StartupPath, unless you care about assembly resolution.
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.
How to get folder path in Command Prompt stand by, not in C# Console App?
When you call Assembly.GetEntryAssembly().Location
then it will return the path of the executing .exe file.
Assuming that Sophairk is the username of your user, then you can do this:
string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
If it is the current directory that you want, then you can do this
string path = Directory.GetCurrentDirectory();
You can check out the documentation here:
https://docs.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-6.0
To see what other "Special Folders" are available.
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.
How can i get folder path in asp.net mvc app by browsing folders
ASP.NET is a web application you can not get your system folder paths. The concept in ASP.NET is to upload the designated file(s) from your system to web server via browse local system. As for folder path if you are developing desktop application using WPF then you can use the .NET FolderBrowserDialog library to browse your system and get the folder path.
Related Topics
How to Encode and Decode a Base64 String
When to Use Static Classes in C#
Is There a Difference Between "Throw" and "Throw Ex"
Input String Was Not in a Correct Format
Simple Insecure Two-Way Data "Obfuscation"
Direct Casting VS 'As' Operator
C# Datetime to "Yyyymmddhhmmss" Format
How to Get the Type of T from a Member of a Generic Class or Method
Create a Strongly Typed C# Object from Json Object With Id as the Name
In C#, Should I Use String.Empty or String.Empty or "" to Intitialize a String
How to Await an Event Instead of Another Async Method
No Connection Could Be Made Because the Target Machine Actively Refused It
Why Are Cdecl Calls Often Mismatched in the "Standard" P/Invoke Convention