how to create .txt file and write it c# asp.net -
i trying create , write text file in c# application using next code
system.io.directory.createdirectory(server.mappath("~\\count")); using (system.io.filestream fs = new system.io.filestream("~/count/count.txt", system.io.filemode.create)) using (system.io.streamwriter sw = new system.io.streamwriter("~/count/count.txt")) { sw.write("101"); } string _count = system.io.file.readalltext("~/count/count.txt"); application["noofvisitors"] = _count;
but error:
the process cannot access file 'path' because beingness used process.
what error?
you're trying open file twice; first using
statements creates filestream
not used, locks file, sec using
fails.
just delete first using
line, , should work fine.
however, i'd recommend replacing of file.writealltext
, there no using in code, it'd much simpler.
var dir = server.mappath("~\\count"); var file = path.combine(dir, "count.txt"); directory.createdirectory(dir); file.writealltext(file, "101"); var _count = file.readalltext(file); application["noofvisitors"] = _count;
c# asp.net text-files
No comments:
Post a Comment