How can I get the current Project Path within the c# build files?

I’m trying to add some compiler definitions that require I get the project directory. More specifically I’m getting the git commit count for my project.

I scoured through some source files to try and find some kind of function or variable to get this information and I found GetUProjectPath. Unfortunately when I try to use it I get this error:

error CS0103: The name 'GetUProjectPath' does not exist in the current context

I’m using it in my ModuleRules constructor for my project inside <Project Name>.Build.cs. I tried using System.IO.Directory.GetCurrentDirectory() but that just gave me the directory of the engine source.

Any insight would be appreciated!

Edit: Just to cover my basics I also did UnrealBuildTool.GetUProjectPath() and get another error.

error CS0234: The type or namespace name 'GetUProjectPath' does not exist in the namespace 'UnrealBuildTool' (are you missing an assembly reference?)

I have pretty much never used C# but my impression is that due to using UnrealBuildTool; being at the top that I don’t need specify the namespace UnrealBuildTool, but I could be wrong. Either way I’m still getting an error.

1 Like

Found a solution to my problem!

There is a variable called ModuleDirectory defined in ModuleRules which gives you the source directory of the project. I just used that to get my root project folder.

edit:
To provide an example I used this in my projects ModuleRules.

public string ProjectRoot
{
    get
    {
           return System.IO.Path.GetFullPath(
               System.IO.Path.Combine(ModuleDirectory, "../../")
           );
    }
}
3 Likes