Error saving file - Already being used by another process [duplicate]

0

I want to read the TextBox text (WinForms) and save it to the file.

It is giving error, saying that the file is being used in another location.

StreamWriter escreverentrada = new StreamWriter(@"escreveentrada.txt",true);
        string valor = valortxt.ToString();
        string nome = nometxt.ToString();
        escreverentrada.WriteLine(valor);
        escreverentrada.Close();
    
asked by anonymous 02.08.2018 / 20:48

1 answer

2

Try to use your StreamWriter within a using block to have Dispose done after using the feature.

using (StreamWriter escreverentrada = new StreamWriter(@"escreveentrada.txt", true))
{
    string valor = valortxt.ToString();
    string nome = nometxt.ToString();
    escreverentrada.WriteLine(valor);
}
    
02.08.2018 / 21:01