How do I write to a file in .NET Core 3.1?

William Johnston 61 Reputation points
2021-06-09T16:13:10.48+00:00

Hello,

I just started using VS 2019 and am new to .NET Core.

My File.WriteAllTextAsync call is not recognized.

The error message is:

1>\program.cs(171,42,171,59): error CS0117: 'File' does not contain a definition for 'WriteAllTextAsync'

I did NuGet the System.IO package.

What should I do?

williamj

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,948 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,162 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,426 Reputation points
    2021-06-09T17:19:35.12+00:00

    The following code sample was done by creating a new .NET Core 3.1 Console project, added the following code and using statement for using System.IO.

    using System;  
    using System.IO;  
    using System.Threading.Tasks;  
      
    namespace ConsoleAppWriteToFile  
    {  
        class Program  
        {  
            static async Task Main(string[] args)  
            {  
                Console.WriteLine("Hello World!");  
      
                await FileOperations.Write("SomeFile.txt", "Line 1\nline2");  
            }  
        }  
      
        public class FileOperations  
        {  
            public static async Task Write(string fileName, string content)  
            {  
                await File.WriteAllTextAsync(fileName, content);  
            }  
        }  
    }  
      
    

    If you expand the Dependencies folder to Frameworks to Microsoft.NETCoreApp you will see System.IO is included.

    103920-figure1.png

    Project file

    <Project Sdk="Microsoft.NET.Sdk">  
      
      <PropertyGroup>  
        <OutputType>Exe</OutputType>  
        <TargetFramework>netcoreapp3.1</TargetFramework>  
      </PropertyGroup>  
      
    </Project>  
      
    

4 additional answers

Sort by: Most helpful
  1. William Johnston 61 Reputation points
    2021-06-09T16:17:14.693+00:00

    I am not sure how to include the System.IO namespace.

    Including 'using System.IO;' is shown greyed out.


  2. Viorel 117.6K Reputation points
    2021-06-09T16:30:06.983+00:00

    Try adding the System.IO.FileSystem package and 'using System.IO'.

    But the function is usually available by default. What kind of project did you create?

    0 comments No comments

  3. Viorel 117.6K Reputation points
    2021-06-09T16:34:34.79+00:00

    If your class is called File too, then rename it, or use the full name: System.IO.File.WriteAllTextAsync(...).

    0 comments No comments

  4. Sam of Simple Samples 5,541 Reputation points
    2021-06-09T17:20:52.827+00:00

    Are you sure you must use the asynchronous version? Asynchronous is more complicated. The following works for me.

    string[] lines = { "First line", "Second line", "Third line" };
    File.WriteAllLines("WriteLines.txt", lines);
    

    You do not need to use NuGet for that. You just need using System.IO;.

    Are you sure you are using .NET Core 3.1? When you right-click on the project (project not solution) in the Solution Explorer what do you see for the Target framework?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.