I am trying to write values to file in C#. I am trying to use either the following code:
using (StreamWriter writer = new StreamWriter("values.txt"))
{
writer.Write("Word");
writer.WriteLine("word 2");
writer.WriteLine("Line");
}
I got the error: Error the best overloaded method match for System.IO.StranWriter.StreamWriter(System.IO.Stream)
has some invalid arguments.
or the following code:
string path = @"values.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
I got the following error: The name 'File' does not exist in the current context
. When am I missing here?
EDIT: Basically I tried to change FIle to System.IO.File and I got the following message:The type or namespace 'File' does not exist in the namespace System.IO(are you missing an assembly reference)
. My code is in a function which I call from public MainPage() function in windows Application.
EDIT2: I tried to follow the solution of the proposed duplicate post. I got the message that await operator can only be used within an async method
.