C# | The process cannot access the file because it is being used by another process, even after closing Binary writer and reader -


i started learning c# about, 3 days ago, , had knowledge of java before.

i've been working on program in these 3 days, hang of c#, , can learn, , that.

in program, it's change few bytes in file, that's not point of this. point is, got of work, , loading bytes , writing bytes file works perfectly, except when.. after close program after changing bytes, , open again, can load them, when try save, error. process cannot access file '(the file)' because being used process.

i absolutely positive it's not being used else, because things have opened opera, visual studio, , program, being debugged. if helps, here important parts (i know, code sucks, i'm new..):

reading/displaying:

choose = names.items.indexof(names.selecteditem); binaryreader br = new binaryreader(file.openread(ofd.filename)); br.basestream.seek(basestats + (28 * choose), seekorigin.begin); hptb.text = br.readbytes(1)[0].tostring(); br.basestream.seek(basestats + (28 * choose) + 1, seekorigin.begin); attb.text = br.readbytes(1)[0].tostring(); br.basestream.seek(basestats + (28 * choose) + 2, seekorigin.begin); deftb.text = br.readbytes(1)[0].tostring(); br.basestream.seek(basestats + (28 * choose) + 3, seekorigin.begin); sptb.text = br.readbytes(1)[0].tostring(); br.basestream.seek(basestats + (28 * choose) + 4, seekorigin.begin); spatb.text = br.readbytes(1)[0].tostring(); br.basestream.seek(basestats + (28 * choose) + 5, seekorigin.begin); spdtb.text = br.readbytes(1)[0].tostring(); br.close(); 

writing:

names.items.indexof(names.selecteditem); binarywriter black = new binarywriter(file.openwrite(ofd.filename)); black.basestream.position = basestats + (choose  * 0x1c); string[] statbox = new string[] { hptb.text, attb.text, deftb.text, sptb.text, spatb.text, spdtb.text }; foreach (string element in statbox) {     black.write((byte)int.parse(element)); } black.close(); 

the exact line error is:

binarywriter black = new binarywriter(file.openwrite(ofd.filename)); 

edit: whole program: http://pastebin.com/mavuld5q (had separate able post)

lines related are: 119, 132, 148, , 156

check out using statements file access.

using (filestream fs = file.openread(path))  {     var br = new binaryreader(fs);     br.basestream.seek(basestats + (28 * choose), seekorigin.begin);     hptb.text = br.readbytes(1)[0].tostring();     br.close(); } 

you'll want file stream disposed. clear, easy way ensure that.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -