How to get Magento 2 base directory programmatically

You can get the Magento 2 base directory the same way as in Magento 1.x: it is stored in the constant BP.

See also other filesystem topics: