I'm trying to use
using (StreamWriter sw = File.AppendText(filePath))
{
sw.WriteLine(n.InnerText);
}
inside a method which has I think must be async, because it's calling a await on an async method GetByteArrayAsync()
PollSite Function:
private async void PollSite(string filePath, string siteURL)
{
response = await http.GetByteArrayAsync(siteURL);
source = WebUtility.HtmlDecode(Encoding
.GetEncoding("utf-8")
.GetString(response, 0, response.Length - 1));
result = new HtmlDocument();
result.LoadHtml(source);
gNode = result.GetElementbyId("SomeTable");
using (StreamWriter sw = File.AppendText(filePath))
{
foreach (HtmlNode n in gNode.Descendants("td"))
{
sw.WriteLine(n.InnerText);
}
}
}
Trying to write gives me the error:
The process cannot access the file '
filePath
' because it is being used by another process.
I'm assuming this is because of the async calls, but have no idea how to get round this and achieve the file write - is it because of the using
statement?