I'm using the following code in my Controller
:
public ActionResult geraBpa()
{
var caminho = System.Web.HttpContext.Current.Server.MapPath("~/Content");
StreamWriter file = new StreamWriter($"{caminho}/BPA.txt");
List<bpac> listaBpac = pegaBpac();
int linhaTexto = 1;
int linhaItem = 1;
foreach (bpac linha in listaBpac)
{
file.WriteLine(
"02" +
linha.cnes +
linha.cmp + //competencia
linha.cbo +
string.Format("{0:000}", linhaTexto) + string.Format("{0:00}", linhaItem) +
linha.pa +
"000" +
string.Format("{0:000000}", linha.quant) +
"EXT"
);
linhaItem++;
if (linhaItem > 99)
{
linhaItem = 1;
linhaTexto++;
}
}
linhaTexto++;
linhaItem = 1;
byte[] fileBytes = System.IO.File.ReadAllBytes($"{caminho}/BPA.txt");
string fileName = "myfile.ext";
return File(fileBytes, MediaTypeNames.Application.Octet, fileName);
}
I'm getting an error on this line:
byte [] fileBytes = System.IO.File.ReadAllBytes ($ "{path} /BPA.txt");
The error says that the BPA.txt
file is open. When I went to the folder, I saw that it was not even created properly.
Any help?