1
The code I am trying to use is the following (I modified only the path and the content), before these lines I do a check if the file does not exist, ie it only executes it if the file does not exist (not to overwrite the same)
File.Create("caminho-arq"); // cria arquivo
File.OpenWrite("caminho-arq"); // abre arquivo para edição
File.WriteAllText("caminho-arq", "conteudo\r\nvai\r\naqui"); // escreve no arquivo
// \r\n começa uma nova linha no arquivo
The line of File.OpenWrite
was my last attempt, even without it does not work, gives error saying "The file 'path-Arq' can not be changed, because it is already being used in another process." Yesterday I faced the same problem, but it wasn’t creating the file, because it was already created, it was meant to read, then save. So I used the StreamRead
, I read and took information and closed the file, then used the File.WriteAllText
to add new information.. But I don’t know how to do it, there is no File.Close()
to close the file
Thanks, I would never think that only one was missing. Close() at the end.. Yes, I used the two just as last try, I knew it wouldn’t work.. I deleted the Openwrite line and added . Close() in Create and it worked perfectly. I’m just waiting here for the time to choose as the right answer :)
– Leonardo
@Leonardovilarinho I improved the answer :)
– Maicon Carraro
I agree
using(StreamWriter sw = File.AppendText("caminho-arq"))
is the best way +1– Guilherme Nascimento