Getting current directory in .NET web application
The current directory is a system-level feature; it returns the directory that the server was launched from. It has nothing to do with the website.
You want HttpRuntime.AppDomainAppPath
.
If you're in an HTTP request, you can also call Server.MapPath("~/Whatever")
.
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);
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 current directory in asp.net mvc
You can use the HttpServerUtility.MapPath method to map any relative paths for you, in your controller this is easily accessible via the ControllerContext
:
string path = ControllerContext.HttpContext.Server.MapPath("~/_xslt/example.xslt");
...
How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac
Depending on where you are in the kestrel pipeline - if you have access to IConfiguration
(Startup.cs
constructor) or IWebHostEnvironment
(formerly IHostingEnvironment
) you can either inject the IWebHostEnvironment
into your constructor or just request the key from the configuration.
Inject IWebHostEnvironment
in Startup.cs
Constructor
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
var contentRoot = env.ContentRootPath;
}
Using IConfiguration in Startup.cs Constructor
public Startup(IConfiguration configuration)
{
var contentRoot = configuration.GetValue<string>(WebHostDefaults.ContentRootKey);
}
Working folder in ASP.NET
To get the path of the root of the application:
//equivalent to Server.MapPath("/"); if at domain root, e.g Http://mysite.com/
string path = Server.MapPath("~");
This answer gives a rundown of a few different common Server.MapPath()
uses that may also be of use to you.
How to get current working directory path c#?
You can use static Directory
class - however current directory is distinct from the original directory, which is the one from which the process was started.
System.IO.Directory.GetCurrentDirectory();
So you can use the following to get the directory path of the application executable:
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
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.
C# How to get current directory path
I will answer my own question, It's not possible to get current path from library project. You'll have to get it from installer. There is no other way.
Thank you.
Related Topics
How Can One Generate and Save a File Client Side Using Blazor
Find() VS. Where().Firstordefault()
Learning Single Responsibility Principle with C#
How to Get a List<String> Collection of Values from App.Config in Wpf
What's the Point of a Lambda Expression
Automated Property with Getter Only, Can Be Set, Why
How to Get Date and Time Formats Based on Culture Info
Long String Interpolation Lines in C#6
Programmatically Encrypting a Config-File in .Net
How to Import from Excel to a Dataset Using Microsoft.Office.Interop.Excel
How to Implement Solid Principles into an Existing Project
Possible to Access MVC Viewbag Object from JavaScript File
How to Await a List of Tasks Asynchronously Using Linq