Introduction of C# Path

In this article I will discuss about the uses of C# Path type.
  • 2190


C# path is a name of a directory which identify a unique location of a file in a file system. A path of A file system is expressed in a string of character. The path component is separated by a delimiting character. There are complications when dealing directly with paths. The .NET Framework provides effective ways of dealing with filenames and paths. It introduces the Path type in the System.IO namespace.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

class Program


    static void Main()


        string path = "C:\\rahul.txt";

        string extension = Path.GetExtension(path);

        Console.WriteLine( extension);

        string filename = Path.GetFileName(path);


        string filenameNoExtension = Path.GetFileNameWithoutExtension(path);

        Console.WriteLine( filenameNoExtension);

        string root = Path.GetPathRoot(path);






The output of following program 


In this example, we take the extension of the file, the actual filename, the filename without the extension, and the path root. The path root will always be "C:\\", with the trailing separator, even when the file is nested in many folders. GetFileName. You can get the filename alone by calling the Path.GetFileName method.

© 2020 DotNetHeaven. All rights reserved.