---
title: "How do you generate a unique file name to avoid overwriting an existing one?"  
description: "How do you generate a unique file name to avoid overwriting an existing one?"  
author: "Ravi Vishwakarma"  
published: 2025-05-12  
updated: 2025-05-12  
canonical: https://www.mindstick.com/interview/34109/how-do-you-generate-a-unique-file-name-to-avoid-overwriting-an-existing-one  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# How do you generate a unique file name to avoid overwriting an existing one?

To **generate a unique file name** and avoid overwriting an existing file in C#, you can use a combination of the file name, a **timestamp**, a **GUID**, or an **incremental suffix**.

### Option 1: Add a timestamp to the file name

```cs
string baseName = "report";
string extension = ".txt";
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string uniqueFileName = $"{baseName}_{timestamp}{extension}";

// Example result: report_20250513_163015.txt
```

### Option 2: Use a GUID

```cs
string uniqueFileName = $"report_{Guid.NewGuid()}.txt";

// Example result: report_3f9c2f1e-874a-42c2-938e-fcf32b73e4cb.txt
```

### Option 3: Check and add a numeric suffix

```cs
string directory = @"C:\Reports";
string baseName = "report";
string extension = ".txt";
string path = Path.Combine(directory, baseName + extension);

int count = 1;
while (File.Exists(path))
{
    path = Path.Combine(directory, $"{baseName}_{count}{extension}");
    count++;
}

// 'path' is now a unique file path
File.WriteAllText(path, "Report content");
```

### Option 4: Use `Path.GetTempFileName()` (for temp files)

```cs
string tempFilePath = Path.GetTempFileName();
// Creates a unique, empty temp file with a .tmp extension
```

### Summary:

| Strategy | Best For |
| --- | --- |
| Timestamp | Human-readable versioning |
| GUID | Guaranteed uniqueness |
| Numeric Suffix | Familiar file naming for users |
| Temp File | Temporary work files |

## Answers

### Answer by Ravi Vishwakarma

To **generate a unique file name** and avoid overwriting an existing file in C#, you can use a combination of the file name, a **timestamp**, a **GUID**, or an **incremental suffix**.

### Option 1: Add a timestamp to the file name

```cs
string baseName = "report";
string extension = ".txt";
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string uniqueFileName = $"{baseName}_{timestamp}{extension}";

// Example result: report_20250513_163015.txt
```

### Option 2: Use a GUID

```cs
string uniqueFileName = $"report_{Guid.NewGuid()}.txt";

// Example result: report_3f9c2f1e-874a-42c2-938e-fcf32b73e4cb.txt
```

### Option 3: Check and add a numeric suffix

```cs
string directory = @"C:\Reports";
string baseName = "report";
string extension = ".txt";
string path = Path.Combine(directory, baseName + extension);

int count = 1;
while (File.Exists(path))
{
    path = Path.Combine(directory, $"{baseName}_{count}{extension}");
    count++;
}

// 'path' is now a unique file path
File.WriteAllText(path, "Report content");
```

### Option 4: Use `Path.GetTempFileName()` (for temp files)

```cs
string tempFilePath = Path.GetTempFileName();
// Creates a unique, empty temp file with a .tmp extension
```

### Summary:

| Strategy | Best For |
| --- | --- |
| Timestamp | Human-readable versioning |
| GUID | Guaranteed uniqueness |
| Numeric Suffix | Familiar file naming for users |
| Temp File | Temporary work files |


---

Original Source: https://www.mindstick.com/interview/34109/how-do-you-generate-a-unique-file-name-to-avoid-overwriting-an-existing-one

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
