console - C# prevent CMD from Closing Automatically after running? -
this question has answer here:
i want prevent cmd closing automatically after running program.
code:
static void main(string[] args) { filestream textfsw = new filestream("fileone.txt", filemode.append, fileaccess.write); // creating file streamwriter textsw = new streamwriter(textfsw); console.write(" enter id: "); int id = int.parse(console.readline()); console.write(" enter name: "); string name = console.readline(); console.writeline(" thank you! logged in as" +name+ +id); textsw.close(); textfsw.close(); filestream textfs = new filestream("fileone.txt", filemode.open, fileaccess.read); // reading file using readalllines streamreader textsr = new streamreader(textfs); string[] lines = file.readalllines("fileone.txt"); }
screenshot:
i want show following after running , prevent cmd closing:
thank you! logged in john smith 12345
any answers appreciated.
thank you.
you can use console.readkey()
, waits until single key pressed (and returns it, don't need in case).
this alternative console.readline()
, console.read()
need user press enter.
as asside, when using file access (and working objects implement idisposable
generally) should use using
statements ensure resources freed in cases:
using(filestream textfs = new filestream("fileone.txt", filemode.open, fileaccess.read)) { using(streamreader textsr = new streamreader(textfs)) { // things textfs , textsr } }
also note since you're using file.readalllines
don't need of above you.