Get the console buffer without the last line with C#? -
what i'm trying achieve self-compiled c# file without toxic output.i'm trying achieve console.movebufferarea method looks not work. eg. - save code below .bat
extension :
// 2>nul||@goto :batch /* :batch @echo off setlocal :: find csc.exe set "frm=%systemroot%\microsoft.net\framework\" /f "tokens=* delims=" %%v in ('dir /b /a:d /o:-n "%systemroot%\microsoft.net\framework\v*"') ( set netver=%%v goto :break_loop ) :break_loop set csc=%frm%%netver%\csc.exe :: csc.exe found %csc% /nologo /out:"%~n0.exe" "%~dpsfnx0" %~n0.exe endlocal exit /b 0 */ public class hello { public static void main() { clearc(); system.console.writeline("hello, c# world!"); } private static void clearc() { system.console.movebufferarea( 0,0, system.console.bufferwidth,system.console.bufferheight-1, 0,0 ); } }
the output be:
c:\>// 2>nul || hello, c# world!
what want rid of // 2>nul ||
.is possible? there wrong in logic (the clearc
method)?do need pinvoke?
if want in c#, changing clearc
function following seems work:
public static void clearc() { system.console.cursortop = system.console.cursortop - 1; system.console.write(new string(' ', system.console.bufferwidth)); system.console.cursortop = system.console.cursortop - 1; }
essentially, move cursor line (to line should contain prompt), blank entire line, move line (which should move blank line between commands). future output take place here.
the obvious downside need wait c# code compiled , executed, before // 2>nul ||
removed. if want faster, you'll need find console/batch file based solution. other thing keep in mind is assumes prompt single line. if it's long prompt spans 2 lines, you'll bit of mess, may better clear 2 lines, depending on how you're planning on using this.
if want go whole hog , start reading console buffer determine how long prompt is, might want have @ question. whilst article link in answer broken, code download still seems work.
if want go down batchfile based approach, might want have @ question.
Comments
Post a Comment