find the parent directory of a directory in csharp

To get the parent directory of a directory in C#, you can use the Directory.GetParent() method which returns the parent directory of the specified directory.

Here's an example code snippet that demonstrates how to use the Directory.GetParent() method to get the parent directory of a directory:

main.cs
using System.IO;

string directoryPath = @"C:\my\directory\path";
string parentDirectoryPath = Directory.GetParent(directoryPath).ToString();

Console.WriteLine("Parent directory path: " + parentDirectoryPath);
211 chars
7 lines

In this example, we first specify the directory path for which we want to find the parent directory. Then we call the Directory.GetParent() method and pass the directory path as an argument to it. Finally, we convert the returned DirectoryInfo object to a string using the ToString() method and store it in a variable called parentDirectoryPath.

This code will output the parent directory path of the specified directory to the console.

gistlibby LogSnag