Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the current project directory from C# code when creating a custom MSBuild task?

Tags:

c#

msbuild

People also ask

What is a project directory?

A project directory provides details of the organisations involved in a design and construction project. This may include their role, organisation name, address, phone numbers, email addresses and names of key contacts.

Where is the Visual Studio project directory?

When you create a new project, Visual Studio saves it to its default location, %USERPROFILE%\source\repos. To change this location, go to Tools > Options > Projects and Solutions > Locations. For more information, see Options dialog box: Projects and Solutions > Locations.

How define file path in C#?

C# Path filename and extensionThe Path. GetFileName returns the file name and extension of a file path represented by a read-only character span. The Path. GetFileNameWithoutExtension returns the file name without the extension of a file path represented by a read-only character span.


using System;
using System.IO;

// This will get the current WORKING directory (i.e. \bin\Debug)
string workingDirectory = Environment.CurrentDirectory;
// or: Directory.GetCurrentDirectory() gives the same result

// This will get the current PROJECT bin directory (ie ../bin/)
string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;

// This will get the current PROJECT directory
string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;

You can try one of this two methods.

string startupPath = System.IO.Directory.GetCurrentDirectory();

string startupPath = Environment.CurrentDirectory;

Tell me, which one seems to you better


If a project is running on an IIS express, the Environment.CurrentDirectory could point to where IIS Express is located ( the default path would be C:\Program Files (x86)\IIS Express ), not to where your project resides.


This is probably the most suitable directory path for various kinds of projects.

AppDomain.CurrentDomain.BaseDirectory

This is the MSDN definition.

Gets the base directory that the assembly resolver uses to probe for assemblies.


This will also give you the project directory by navigating two levels up from the current executing directory (this won't return the project directory for every build, but this is the most common).

System.IO.Path.GetFullPath(@"..\..\")

Of course you would want to contain this inside some sort of validation/error handling logic.


If you want ot know what is the directory where your solution is located, you need to do this:

 var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
            if (parent != null)
            {
                var directoryInfo = parent.Parent;
                string startDirectory = null;
                if (directoryInfo != null)
                {
                    startDirectory = directoryInfo.FullName;
                }
                if (startDirectory != null)
                { /*Do whatever you want "startDirectory" variable*/}
            }

If you let only with GetCurrrentDirectory() method, you get the build folder no matter if you are debugging or releasing. I hope this help! If you forget about validations it would be like this:

var startDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;