---
title: "How do you create a nested directory structure in one line?"  
description: "How do you create a nested directory structure in one line?"  
author: "ICSM Computer"  
published: 2025-05-11  
updated: 2025-05-11  
canonical: https://www.mindstick.com/interview/34104/how-do-you-create-a-nested-directory-structure-in-one-line  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 1 minute  

---

# How do you create a nested directory structure in one line?

You can create a **nested directory structure in one line** in C# using:

```cs
Directory.CreateDirectory(@"C:\MainFolder\SubFolder1\SubFolder2");
```

### How it works:

1. If any intermediate directories (like `MainFolder` or `SubFolder1`) **don’t exist**, they are automatically created.
2. If the directory **already exists**, no error is thrown — it's safe to call repeatedly.

### Example:

```cs
using System.IO;

class Program
{
    static void Main()
    {
        Directory.CreateDirectory(@"C:\Logs\2025\May\08");
    }
}
```

This creates all folders (`Logs`, `2025`, `May`, `08`) if they don't exist.

## Answers

### Answer by ICSM Computer

You can create a **nested directory structure in one line** in C# using:

```cs
Directory.CreateDirectory(@"C:\MainFolder\SubFolder1\SubFolder2");
```

### How it works:

1. If any intermediate directories (like `MainFolder` or `SubFolder1`) **don’t exist**, they are automatically created.
2. If the directory **already exists**, no error is thrown — it's safe to call repeatedly.

### Example:

```cs
using System.IO;

class Program
{
    static void Main()
    {
        Directory.CreateDirectory(@"C:\Logs\2025\May\08");
    }
}
```

This creates all folders (`Logs`, `2025`, `May`, `08`) if they don't exist.


---

Original Source: https://www.mindstick.com/interview/34104/how-do-you-create-a-nested-directory-structure-in-one-line

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
