Introduction of C# Path

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

 Introduction

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.

Example

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);

        Console.WriteLine(filename);

        string filenameNoExtension = Path.GetFileNameWithoutExtension(path);

        Console.WriteLine( filenameNoExtension);

        string root = Path.GetPathRoot(path);

        Console.WriteLine(root);

        Console.ReadLine();

        }

    }

 

The output of following program 

 Clipboard111.jpg

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.