user1144852
user1144852

Reputation: 265

How to store only file name not the whole path using c#?

In my c# code, I am reading file name to a variable and they could be like C:\users\documetnts\test.bin but, how can I get rid of the whole path i just want to store file name test.bin not the whole path.

How can I do this?

Upvotes: 0

Views: 44

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

Use the Path.GetFileName Method from the Path Class in the System.IO Namespace:

class Program
{
    static void Main(string[] args)
    {
        string path = @"C:\users\documetnts\test.bin";

       Console.WriteLine( Path.GetFileName(path));
       Console.ReadLine();
    }
}

Upvotes: 2

Related Questions