Thursday, June 18, 2026

C# What is difference between Path and PathString

What is difference between Path and PathString class?

In C#, Path and PathString are completely different types and are used in different contexts.

Feature Path PathString
Namespace System.IO Microsoft.AspNetCore.Http
Type static class struct
Purpose Work with file system paths Work with HTTP URL paths
Used in Desktop apps, console apps, file handling ASP.NET Core web apps
Represents File/folder paths URL request path
Example C:\Docs\file.txt /products/details
  1. Path → File and directory path utilities

System.IO.Path contains static methods to manipulate filesystem paths.

Example:

string full = @"C:\Docs\report.pdf";
Console.WriteLine(Path.GetFileName(full));       // report.pdf
Console.WriteLine(Path.GetExtension(full));      // .pdf
Console.WriteLine(Path.GetDirectoryName(full));  // C:\Docs
Console.WriteLine(Path.Combine("C:\\Docs", "a.txt"));

Common methods:

  • Path.Combine()
  • Path.GetFileName()
  • Path.GetExtension()
  • Path.GetDirectoryName()
  • Path.ChangeExtension()
  • Path.GetTempPath()

Path itself does not store a path.

  1. PathString → URL path wrapper for ASP.NET Core

PathString represents the path part of an HTTP request URL.

Example URL:

https://example.com/products/123?sort=asc

Parts:

  • Scheme: https
  • Host: example.com
  • Path: /products/123   ← PathString
  • Query: ?sort=asc

Example:

app.Use(async (context, next) =>
{
    PathString path = context.Request.Path;
 
    if (path.StartsWithSegments("/api"))
    {
        Console.WriteLine("API request");
    }
 
    await next();
});

Useful methods:

  • StartsWithSegments()
  • Add()
  • FromUriComponent()
  • ToUriComponent()

Example:

PathString p1 = "/users";
PathString p2 = "/123";
Console.WriteLine(p1.Add(p2));// /users/123

One important difference

Path.Combine() automatically uses OS separators:

Path.Combine("docs", "file.txt")
// Windows → docs\file.txt
// Linux → docs/file.txt

PathString always uses URL style:

new PathString("/docs/file.txt")

So:

  • Use Path → when dealing with files and folders
  • Use PathString → when dealing with web request URLs

C# functionalities in Directory class which are not available in DirectoryInfo class and vice versa

What are special functionalities in Directory class which are not available in DirectoryInfo class and vice versa?

Directory and DirectoryInfo overlap a lot, but they are designed differently.

  • Directory → static utility class (you work with paths as strings)
  • DirectoryInfo → instance-based class (you work with a directory object)

👉Most operations exist in both. The main differences are about style, state, and convenience, not unique capabilities.

Functionalities in Directory that DirectoryInfo does NOT have

  1. Static methods (no object creation)

You don't need to create a DirectoryInfo.

Directory.CreateDirectory(@"C:\Data");
Directory.Delete(@"C:\Temp");

Equivalent with DirectoryInfo:

DirectoryInfo dir = new DirectoryInfo(@"C:\Data");
dir.Create();

Useful when:

  • You only need a one-time operation
  • You already have path strings
  1. Current directory management

Directory contains application-level operations.

Directory.SetCurrentDirectory(@"C:\Projects");
string current = Directory.GetCurrentDirectory();

DirectoryInfo has no equivalent.

  1. Logical drive root helper
string root = Directory.GetDirectoryRoot(@"D:\Work\Test");

No equivalent in DirectoryInfo.

Functionalities in DirectoryInfo that Directory does NOT have

  1. Cached directory metadata (inherits from FileSystemInfo)

You can access properties directly:

DirectoryInfo dir = new DirectoryInfo(@"C:\Data");
Console.WriteLine(dir.CreationTime);
Console.WriteLine(dir.LastWriteTime);
Console.WriteLine(dir.Attributes);
Console.WriteLine(dir.Exists);
Directory has methods for some of these:
Directory.GetCreationTime(path);

But it doesn't hold state.

  1. Parent directory access
DirectoryInfo dir =  new DirectoryInfo(@"C:\Data\Test");
Console.WriteLine(dir.Parent?.Name);

No direct Directory.Parent is available.

  1. Navigate object hierarchy
DirectoryInfo dir = new DirectoryInfo(@"C:\Data");
DirectoryInfo[] subdirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();

You immediately get DirectoryInfo / FileInfo objects.

With Directory:

string[] dirs = Directory.GetDirectories(path);

Only strings are returned.

  1. Rename using MoveTo()
DirectoryInfo dir =  new DirectoryInfo(@"C:\Old");
dir.MoveTo(@"C:\New");

Directory.Move() exists too, but instance style can feel cleaner.

Example where DirectoryInfo becomes nicer

The following LINQ example repeatedly creates DirectoryInfo.

Instead of:

Directory.GetDirectories(path)
         .Select(x => new DirectoryInfo(x))

Prefer:

DirectoryInfo dir =  new DirectoryInfo(path);
foreach (DirectoryInfo d in dir.GetDirectories())
{
    Console.WriteLine(d.Name);
}

No repeated object creation.

Rule of thumb

Use Prefer
One quick operation Directory
Multiple operations on same folder DirectoryInfo
Need metadata (Parent, CreationTime) DirectoryInfo
Need current working directory Directory

So DirectoryInfo isn't more powerful—it mainly provides an object-oriented wrapper over directory operations plus metadata caching through FileSystemInfo.

 

Hot Topics