Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# can not write to file ("being used by another process")?

Tags:

c#

file

exception

I was hoping someone could explain to me why this is happening :)

When I use the code below it gives me the error "The process cannot access the file 'C:\test.txt' because it is being used by another process."

I'm very new to C# so I'm not sure what is going on, thanks in advance!

String fileNameBefore = @"C:\\test.txt";

public void output(String hex)
{
    using (StreamWriter writer = new StreamWriter(fileNameBefore, true))
    {
        writer.Write(hex);
        writer.Close();
    }
}
like image 225
James Avatar asked Jun 05 '26 19:06

James


1 Answers

Close the text file before you write to it. (Not in code...physically close the text file)

like image 195
MyKuLLSKI Avatar answered Jun 07 '26 08:06

MyKuLLSKI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!