# Filer och mappar

Alla dessa kräver att du först inkluderar System.IO i filen där du vill använda dem.

```csharp
using System.IO;
```

## Filer

### File.Delete()

Tar bort den fil som anges som parameter.

```csharp
File.Delete(@"localfile.txt");
```

### File.Exists()

File.Exists är en metod som returnerar true om filen som anges som parameter existerar, false om den inte gör det.

```csharp
if (File.Exists(@"localfile.txt"))
{
  Console.WriteLine("The file exists!");
}
```

## Mappar

### Directory.CreateDirectory()

Skapa en mapp.

```csharp
Directory.Create(@"Savegames");
```

### Directory.Delete()

Ta bort en mapp.

```csharp
Directory.Delete(@"Savegames");
```

### Directory.Exists()

Kolla om en mapp existerar.

{% code lineNumbers="true" %}

```csharp
if (Directory.Exists(@"Savegames")
{
  /* Stuff */
}
```

{% endcode %}

### Directory.GetFiles()

Hämta en array som innehåller alla filer som finns i en mapp.

{% code lineNumbers="true" %}

```csharp
foreach (string filename in Directory.GetFiles(@"Savegames")
{
  Console.WriteLine(filename);
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://csharp.progdocs.se/filhantering/filer-och-mappar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
