---
title: "How can you use Path.Combine() and Path.GetExtension() when working with file paths?"  
description: "How can you use Path.Combine() and Path.GetExtension() when working with file paths?"  
author: "ICSM Computer"  
published: 2025-05-07  
updated: 2025-05-30  
canonical: https://www.mindstick.com/forum/161589/how-can-you-use-path-combine-and-path-getextension-when-working-with-file-paths  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How can you use Path.Combine() and Path.GetExtension() when working with file paths?

How can you use `Path.Combine()` and `Path.GetExtension()` when working with [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) paths?

## Replies

### Reply by ICSM Computer

Great question! Here's how you use `Path.Combine()` and `Path.GetExtension()` in C# when working with file paths:

## 1. `Path.Combine()`

**Purpose:**\
Safely join multiple parts of a path into one correctly formatted path, handling directory separators automatically (so you don’t need to worry about trailing slashes or extra separators).

### Example:

```cs
using System.IO;

string folder = @"C:\Users\Alice\Documents";
string filename = "report.pdf";

string fullPath = Path.Combine(folder, filename);
// Result: C:\Users\Alice\Documents\report.pdf
```

You can combine multiple parts:

```cs
string subfolder = "2025";
string fullPath = Path.Combine(folder, subfolder, filename);
// Result: C:\Users\Alice\Documents\2025\report.pdf
```

## 2. `Path.GetExtension()`

**Purpose:**\
Retrieve the file extension (including the dot) from a file path or file name.

### Example:

```cs
string filePath = @"C:\Users\Alice\Documents\report.pdf";

string extension = Path.GetExtension(filePath);
// Result: ".pdf"
```

If the file has no extension:

```cs
string filePath = @"C:\Users\Alice\Documents\README";

string extension = Path.GetExtension(filePath);
// Result: "" (empty string)
```

## Combined Usage Example:

```cs
string baseDir = @"C:\Files";
string subDir = "images";
string filename = "photo.jpg";

string path = Path.Combine(baseDir, subDir, filename);

string ext = Path.GetExtension(path); // ".jpg"
```

### Summary

| Method | Purpose | Returns |
| --- | --- | --- |
| `Path.Combine()` | Join multiple path segments | Combined full path string |
| `Path.GetExtension()` | Extract file extension from a path | File extension including `.` or empty string |


---

Original Source: https://www.mindstick.com/forum/161589/how-can-you-use-path-combine-and-path-getextension-when-working-with-file-paths

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
