Error while saving file - Already being used by another process

Asked

Viewed 975 times

0

I want to read the text of Textbox (Winforms) and save to the file.

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();
  • The error itself tells the problem... it is open in some method. Give a look

  • In fact, I created the file in this method there, and would not have the option to use elsewhere. Because it is a private method.

  • If you created it by the app you must have forgotten to give Dispose().

  • And the reading of the Textlabel right there?

  • Or rather, from TEXTBOX

1 answer

2

Try using your Streamwriter within a block using to have Dispose done after using the resource.

using (StreamWriter escreverentrada = new StreamWriter(@"escreveentrada.txt", true))
{
    string valor = valortxt.ToString();
    string nome = nometxt.ToString();
    escreverentrada.WriteLine(valor);
}
  • Thanks bro, I’m new to this programming life. Thank you, I think it worked!

  • Not at all! If you want to understand a little better about the use of the "using" instruction, I recommend reading the official Microsoft material. Follow the link: https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/keywords/using-statement

Browser other questions tagged

You are not signed in. Login or sign up in order to post.