find the name of a directory in csharp

To find the name of a directory in C#, you can use the Path class provided by the .NET framework. Specifically, you can use the Path.GetFileName() method to get the name of the directory from a given path.

Here is an example code snippet that demonstrates how to find the name of a directory using Path.GetFileName():

main.cs
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string path = @"C:\Users\JohnDoe\Documents\MyDirectory";
        string directoryName = Path.GetFileName(path);
        Console.WriteLine("Directory name is: " + directoryName);
    }
}
284 chars
13 lines

In this example, we first define a path to a directory in the path variable. We then call Path.GetFileName() on this path to get the directory name, which is stored in the directoryName variable. Finally, we print the directory name to the console using Console.WriteLine().

gistlibby LogSnag