I'm having a small problem implementing async methods into my code as it is my first time using it. I am having trouble writing to a file, when I make the method synchronous it works fine, however, when using async - nothing happens.
Here is the code I am currently using:
static async Task WriteFile()
{
string path = $@"C:\Users\{Environment.UserName}\FileToWrite.js";
if (File.Exists(path))
{
try
{
string value;
using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("AsyncTest.Resources.data.txt")))
{
value = await reader.ReadToEndAsync();
}
using (StreamWriter writer = new StreamWriter(path))
{
await writer.WriteAsync(value);
}
}
catch
{
Debug.WriteLine("Error writing to file.");
}
}
}
I am calling this method like this
await WriteFile(path);
Data in Resources.data.txt:
This is a test, the text file should contain this message.
Any help would be greatly appreciated, thank you!