# FileHandle

> A handle to an asynchronously opened file.

## Definition

* **Type:** Struct
* **Namespace:** [Unity.IO.LowLevel.Unsafe](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public readonly struct FileHandle
```

## Remarks

Opening a file with [AsyncReadManager.OpenFileAsync](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager/openfileasync.md) returns a FileHandle instance. You can use this handle to check the status of the asynchronous open operation.

Use [AsyncReadManager.Read](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager/read.md) to read the data in the file after the open operation is complete. This will automatically wait for the open operation to complete, and give the ReadHandle a WaitingOnJob ReadStatus while it does so. If using [AsyncReadManager.ReadDeferred](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager/readdeferred.md) instead, you should make sure that the passed in JobHandle waits on this [FileHandle.JobHandle](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/filehandle/jobhandle.md) to schedule the read job after the open operation finishes.

Always call [FileHandle.Close](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/filehandle/close.md) on the FileHandle when finished to avoid memory leaks and holding a file open. You must call close even if the open operation failed, to dispose of the FileHandle.

To write to a file, use the standard .NET File APIs, such as System.IO.StreamWriter. You must close this FileHandle before you can read or write to the file with other APIs. (If a file is held open by the [AsyncReadManager](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager.md)'s file cache, you can use [AsyncReadManager.CloseCachedFileAsync](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager/closecachedfileasync.md) to close it, but do not use that API to close files for which you have a FileHandle as these are not in the cache.)

## Properties

| Property                                                                                      | Description                                                                                                                                                                                                                                     |
| --------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [JobHandle](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/filehandle/jobhandle.md) | The JobHandle of the asynchronous file open operation begun by the call to [AsyncReadManager.OpenFileAsync](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager/openfileasync.md) that returned this FileHandle instance. |
| [Status](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/filehandle/status.md)       | The current status of this FileHandle.                                                                                                                                                                                                          |

## Methods

| Method                                                                                    | Description                                                                                        |
| ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| [Close](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/filehandle/close.md)     | Asynchronously closes the file referenced by this FileHandle and disposes the FileHandle instance. |
| [IsValid](/engine/6000.6/script-reference/unity/io/lowlevel/unsafe/filehandle/isvalid.md) | Reports whether this FileHandle instance is valid.                                                 |
