ZetCode

C# Path

last modified July 5, 2020

C# Path tutorial shows how to work with file and directory path information in C#. The operations are performed in a cross-platform manner. C# tutorial is a comprehensive tutorial on C# language.

The Path is located in the System.IO namespace. With the Path class, we can easily figure out the root path, the directory name of the file, its extension or create a random file name.

C# Path.GetPathRoot

The Path.GetPathRoot method returns the root directory information from the path contained in the specified character span.

Program.cs
using System;
using System.IO;

namespace RootPath
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = "/home/janbodnar/tmp/";
            var root = Path.GetPathRoot(path);

            Console.WriteLine(root);
        }
    }
}

The example prints the root path of the specified path.

$ dotnet run
/

This is a sample output.

C# Path.GetDirectoryName

The Path.GetDirectoryName returns the directory information for the specified path represented by a character span.

Program.cs
using System;
using System.IO;

namespace DirectoryName
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = "/home/janbodnar/words.txt";
            var dirName = Path.GetDirectoryName(path);

            Console.WriteLine(dirName);
        }
    }
}

The example prints the directory name of the specified path.

$ dotnet run
/home/janbodnar

This is a sample output.

C# Path.GetFullPath

The Path.GetFullPath returns the absolute path for the specified path string.

Program.cs
using System;
using System.IO;

namespace FullPath
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = ".";
            var fullPath = Path.GetFullPath(path);

            Console.WriteLine(fullPath);
        }
    }
}

The example prints the full path of the current working directory.

$ dotnet run
/root/Documents/prog/c#/path/FullPath

This is a sample output.

C# Path.GetRandomFileName

The Path.GetRandomFileName returns a random directory or file name.

Program.cs
using System;
using System.IO;

namespace RandomFileName
{
    class Program
    {
        static void Main(string[] args)
        {
            var randFileName = Path.GetRandomFileName();
            Console.WriteLine(randFileName);
        }
    }
}

The example prints an example of a randomly generated file name.

$ dotnet run
j1wtvfxj.zrh

This is a sample output.

C# Path filename and extension

The Path.GetExtension returns the extension (including the period) of the specified path string. The Path.GetFileName returns the file name and extension of a file path represented by a read-only character span. The Path.GetFileNameWithoutExtension returns the file name without the extension of a file path represented by a read-only character span.

Program.cs
using System;
using System.IO;

namespace FileNameExtension
{
    class Program
    {
        static void Main(string[] args)
        {            
            var path = "/home/janbodnar/words.txt";

            var fileExt = Path.GetExtension(path);
            Console.WriteLine(fileExt);

            var fileName = Path.GetFileName(path);
            Console.WriteLine(fileName);

            var fileNameWithoutExt = Path.GetFileNameWithoutExtension(path);
            Console.WriteLine(fileNameWithoutExt);

        }
    }
}

The example prints the extension, the file name, and the file name without the extension of the specified path.

$ dotnet run
.txt
words.txt
words

This is the output.

C# Path.Combine

The Path.Combine combines strings into a path.

Program.cs
using System;
using System.IO;

namespace PathCombine
{
    class Program
    {
        static void Main(string[] args)
        {
            var fullPath1 = Path.Combine("/home", "janbodnar", "words.txt");
            Console.WriteLine(fullPath1);

            var fullPath2 = Path.Combine("/home/janbodnar/", 
                "/home/janbodnar/words2.txt");
            Console.WriteLine(fullPath2);
        }
    }
}

The example concatenates individual strings into a single string that represents a file path.

$ dotnet run
/home/janbodnar/words.txt
/home/janbodnar/words2.txt

This is the output.

In this tutorial we have worked with path strings in C#.

Read C# tutorial or list all C# tutorials.