# Writing to File

### 1. Write to file

Once you have added all the data to the `OutputTable`, you can write the data to a file. The output data file is in .csv format and is stored in the solution path Output folder. After registering the data table in the `OutputManager`, it can be written to a file using the `WriteToFile` method.

```csharp
OutputTable outputTable = new OutputTable();

SampleOutputData row = new SampleOutputData(); // Create data row

row.COL_1 = 1;
row.COL_2 = 2;

outputTable.AddRow(row); // Add data row to table

OutputManager.Instance.SetOutput(outputTable.Name, outputTable); // Register table in OutputManager
outputTable.WriteToFile(); // Write to .csv file
```

{% hint style="info" %}
When writing a file, if a file with the same name exists in the path, it defaults to deleting the existing file and creating a new one.
{% endhint %}

When writing to a file, the `WriteToFile` method provides the following functionality

#### Appending to the last line of an existing file

If a file with the same filename exists, you can append to the contents of the existing file to output data as needed without deleting the existing file. This can be accomplished by setting the value of the **isAppend** parameter of the `WriteToFile` method to True. The example code below outputs data following the last line of an existing file.

```csharp
outputTable.WriteToFile(path, true); // Append data to an existing file
```

#### Setting whether to add column names for the first row

When writing to a file, the default is to write the column names of the corresponding output data schema in the first row. However, you can choose not to write the column names in the first row as needed. You can accomplish this by setting the value of the **isHeaderInclude** parameter of the `WriteToFile` method to False. The example code below prevents the column names from being written to the first row.

```csharp
outputTable.WriteToFile(path, false, false); // Not include header (column names) 
```

#### Setting file name

When writing to a file, it defaults to writing the file name to be the same as the output data schema name. Users can change the output file name as needed. This can be accomplished by setting the file name you want to change in the value of the **name** parameter of the `WriteToFile` method. The example code below renames the file to "Result".

```csharp
outputTable.WriteToFile(path, false, true, "Result"); // Set file name to "Result"
```

### 2. Example code

The following example code outputs the contents of the Best Solution in the [`OnDoneSolve`](https://swonh.gitbook.io/sdmp-user-manual-eng/general-module/user-controls/eventcontrol#ondonesolve) method, which is called after the end of the Solver run. It defines the Result output data schema to correspond to the Solution structure and adds data rows to the `OutputTable`. After adding the data is complete, write that data table to a file.

```csharp
public override void OnDoneSolve()
{
    OutputManager outputManager = OutputManager.Instance;

    OutputTable resultsTable = new OutputTable();

    SolutionManager solutionManager = SolutionManager.Instance;
    Solution bestSol = solutionManager.BestSolution;

    IOrderedEnumerable<KeyValuePair<int, State>> states = bestSol.States.OrderBy(x => x.Key);

    int seq = 1;
    foreach (KeyValuePair<int, State> item in states)
    {
        CRPState state = item.Value as CRPState;

        if (state.IsInitial)
            continue;

        Result row = new Result();

        row.SEQUENCE = seq;
        row.CONVEYOR = state.CurrentConveyor.ConveyorNum;
        row.JOB = state.LastRetrievedJob.Number;
        row.COLOR = state.LastRetrievedJob.Color.ColorNumber;

        resultsTable.AddRow(row);

        seq++;
    }

    outputManager.SetOutput(resultsTable.Name, resultsTable);
    resultsTable.WriteToFile();
}
```
