Get console buffer without last line with C #?

What I'm trying to achieve is a self-compiled C # file without toxic output. I am trying to achieve this using the Console.MoveBufferArea method, but it doesn't seem to work. For example. - save the code below with the extension .bat :

 // 2>nul||@goto :batch /* :batch @echo off setlocal :: find csc.exe set "frm=%SystemRoot%\Microsoft.NET\Framework\" for /f "tokens=* delims=" %%v in ('dir /b /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do ( 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 will be:

 C:\>// 2>nul || Hello, C# World! 

What you need is to get rid of // 2>nul || . Is it possible? Is there something wrong with my logic ( ClearC method)? Do I need PInvoke?

+8
c #
source share
1 answer

If you want to do this in C #, then perhaps change your ClearC function to the following:

 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 the cursor up the line (to the line that should contain your prompt), close the entire line, then move to another line (which should move you to the empty line between the commands). Then a future exit will take place.

The obvious drawback is that you need to wait for the C # code to be compiled and executed before deleting // 2>nul || . If you want this to be faster, you need to find a solution based on the console / batch file. Another thing to keep in mind is that the invitation is assumed to be a single line. If this is a really long prompt that spans two lines, then you get a little mess, so it might be better to clear the two lines, depending on how you plan to use this.

If you want to go through the whole hog and start reading the console buffer to determine how long it will take, then you can take a look at this question . As long as the link to the article in the answer does not work, loading the code still works.

If you want to move on to a batchfile based approach, then you might want to take a look at this question .

+2
source share

All Articles