---
title: "How do you handle long file paths (over 260 characters) in .NET Framework and .NET Core?"  
description: "How do you handle long file paths (over 260 characters) in .NET Framework and .NET Core?"  
author: "ICSM Computer"  
published: 2025-05-16  
updated: 2025-05-16  
canonical: https://www.mindstick.com/interview/34130/how-do-you-handle-long-file-paths-over-260-characters-in-dot-net-framework-and-dot-net-core  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# How do you handle long file paths (over 260 characters) in .NET Framework and .NET Core?

Handling **long file paths (over 260 characters)** requires different approaches in **.NET Framework** vs **.NET Core** due to historical limitations.

## Why the 260 Character Limit?

Windows historically imposed a `MAX_PATH` limit of 260 characters. In .NET Framework, this limit is enforced unless you use special workarounds. .NET Core and .NET 5+ have better support for long paths by default.

## .NET Framework (≤ 4.6.2)

### Problem:

By default, paths longer than 260 characters cause an error like:

```cs
System.IO.PathTooLongException
```

### Workaround:

Use **UNC (long path) prefix**: `\\?\` before absolute paths.

```cs
string longPath = @"\\?\C:\very\long\path\to\your\file.txt";
File.ReadAllText(longPath);
```

### Requirements:

1. Must use full (absolute) paths.
2. Works only with local paths, not relative paths.
3. Limited support in many .NET APIs.

## .NET Core / .NET 5+ / .NET 6+

.NET Core and .NET 5+ fully support long paths **without requiring** `\\?\` if the OS supports it and it's enabled.

### Ensure Windows long paths are enabled:

1. Open `gpedit.msc`
2. Navigate to:\ `Computer Configuration > Administrative Templates > System > Filesystem`
3. Enable: **Enable Win32 long paths**

Or modify registry:

```cs
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001
```

### Then use normally:

```cs
string path = @"C:\some\very\long\directory\structure\...file.txt";
File.WriteAllText(path, "data");
```

## Tips

1. Prefer `.NET Core` or later for better long path support.
2. Avoid long path issues by keeping directory structures flat when possible.
3. Use `Path.GetFullPath()` to debug unexpected length problems.

## Answers

### Answer by ICSM Computer

Handling **long file paths (over 260 characters)** requires different approaches in **.NET Framework** vs **.NET Core** due to historical limitations.

## Why the 260 Character Limit?

Windows historically imposed a `MAX_PATH` limit of 260 characters. In .NET Framework, this limit is enforced unless you use special workarounds. .NET Core and .NET 5+ have better support for long paths by default.

## .NET Framework (≤ 4.6.2)

### Problem:

By default, paths longer than 260 characters cause an error like:

```cs
System.IO.PathTooLongException
```

### Workaround:

Use **UNC (long path) prefix**: `\\?\` before absolute paths.

```cs
string longPath = @"\\?\C:\very\long\path\to\your\file.txt";
File.ReadAllText(longPath);
```

### Requirements:

1. Must use full (absolute) paths.
2. Works only with local paths, not relative paths.
3. Limited support in many .NET APIs.

## .NET Core / .NET 5+ / .NET 6+

.NET Core and .NET 5+ fully support long paths **without requiring** `\\?\` if the OS supports it and it's enabled.

### Ensure Windows long paths are enabled:

1. Open `gpedit.msc`
2. Navigate to:\ `Computer Configuration > Administrative Templates > System > Filesystem`
3. Enable: **Enable Win32 long paths**

Or modify registry:

```cs
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001
```

### Then use normally:

```cs
string path = @"C:\some\very\long\directory\structure\...file.txt";
File.WriteAllText(path, "data");
```

## Tips

1. Prefer `.NET Core` or later for better long path support.
2. Avoid long path issues by keeping directory structures flat when possible.
3. Use `Path.GetFullPath()` to debug unexpected length problems.


---

Original Source: https://www.mindstick.com/interview/34130/how-do-you-handle-long-file-paths-over-260-characters-in-dot-net-framework-and-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
