string filePath = @"C:\Examples\Files\Example.txt"; string parentFilePath = Path.GetDirectoryName(Path.GetDirectoryName(filePath)); Console.WriteLine(parentFilePath);
string filePath = "/usr/local/bin/example"; string parentFilePath = Path.GetDirectoryName(Path.GetDirectoryName(filePath)); Console.WriteLine(parentFilePath);This code example demonstrates how to use the GetParentFile method to obtain the parent folder path of a file located on a Linux-based file system. The Path.GetDirectoryName method is used twice to navigate up two levels from the file path. Package/library name: System.IO.FileSystem. The Sharpen FilePath GetParentFile method is part of the System.IO.FileSystem library, which is included in the .NET framework. This library provides developers with a set of tools for working with files and directories on the file system.