How to write string to file in C#

In this article, we are going to discuss how to write string to file in C#. There are multiple ways to do this task but firstly we will discuss static convenience methods on the System.IO.File class to write IEnumerable<string> and a string to a text file.

How write array of strings in file in C#:

using System.IO;
using System.Threading.Tasks;

class demo
{
    public static async Task ExampleAsync()
    {
        string[] lines =
        {
            "dummy line 1", "dmmy line 2", "dummy line 3" 
        };

        await File.WriteAllLinesAsync("WriteLines.txt", lines);
    }
}

In the above example, we are writing the array of strings after creating a file and if the file does exist before overwriting the file using File.WriteAllLinesAsync(). After writing the file, closing and disposing of it as required.

How write one string in file in C#:

using System.IO;
using System.Threading.Tasks;

class demo
{
    public static async Task ExampleAsync()
    {
        string text =
            "This is dummy text 1, " +
            "This is dummy text 2 ";

        await File.WriteAllTextAsync("demo.txt", text);
    }
}

In the above example, we have written a single string using File.WriteAllTextAsync() method into a file. It’s creating a file demo.txt and if this file exists, overwriting the file. After writing the file, closing and disposing of it as required.

How to write selected string from an array to file in C#:

using System.IO;
using System.Threading.Tasks;

class demo
{
    public static async Task ExampleAsync()
    {
        string[] lines = { "dem line1", "demo line2", "demo line3" };
        using StreamWriter file = new("demo.txt");

        foreach (string line in lines)
        {
            if (!line.Contains("line2"))
            {
                await file.WriteLineAsync(line);
            }
        }
    }
}

In the above example, we have instantiated an array of strings and then we have instantiated the StreamWriter with the file path demo.txt. After that, we applied a foreach loop to the array and conditioned the loop to write the line which does not contain “line2”. In the If statement, we have written the file with file.WriteLineAsync() method. After writing the file, closing and disposing of it as required.

How to Append text to an existing file in C#:

using System.IO;
using System.Threading.Tasks;

class demo
{
    public static async Task ExampleAsync()
    {
        using StreamWriter file = new("demo.txt", append: true);
        await file.WriteLineAsync("dummy text line");
    }
}

In the above example, we have instantiated the StreamWriter with the file path demo.txt setting the append to “true”. we have written the file with file.WriteLineAsync() method. After writing the file, closing and disposing of it as required.

I have also written a detailed tutorial on How to throw Custom Exceptions in ASP.NET Core Web API.

Conclusion:

In this article, we have discussed multiple ways How to write string to file in C#. If you face any issues or have any suggestion to make our past better. Do not hesitate to comment below or contact.

Leave a Reply

Your email address will not be published.