C# Console Application Individual Line Loading - c#

I'm currently working in C# in visual studio 2010. I am working on console applications and was wondering if anyone knew a way to have Console.WriteLines appear on the console individually without key strokes? E.g. text that appears line by line. I've looked around quite a bit and can't seem to find a way to do it without using standard command lines rather than C#.
Thanks in advance.

For each string you want on its own line, write that string to the console.
string a = "hello";
string b = "world";
Console.WriteLine(a);
Console.WriteLine(b);

Try to put
Console.ReadKey()
between your line outputs. This will wait for any key to be pressed after every line.
Example:
Console.Write("Press any key to continue ...");
Console.ReadKey(true);
Console.WriteLine("DONE!")
This will print "Press any key to continue ..." then wait for any keystroke and add "DONE!" to the end of the line.

Thanks to everyone who replied, sorry it look my a long time to get back to this, I've been extremely busy. Sorry that my question was poorly phrased. What I was attempting to do was create an image with console.WriteLine();
I found that the way to do this was with system threading to create a delay between lines.

Related

C# Check for key press without delay (Console application) [duplicate]

This question already has answers here:
Listen on ESC while reading Console line
(6 answers)
Closed 1 year ago.
first time posting here, so I apologise in advance in case I miss something important, please let me know if that's the case!
I'm writing a console application that has a menu with several different options in different layers, and I'm trying to make a function that will, at any time as the user works in these menus, notice if the Esc key is pressed. To make my code less cluttered I wrote a separate method that I can simply call at any point when I'm asking for input (like a choice, or entering information into a form, or such).
The problem I have is that it always cancels out the first key press. If a user enters a number to move in the menu, the number doesn't get entered, if they enter a name into a form the first letter is missing etc.
Is there a way around this?
The code currently looks like this;
public static void checkForEsc()
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape)
{
switch (currentUserisAdmin)
{
case true:
Menu.AdminMenu();
break;
case false:
Menu.CustomerMenu();
break;
}
}
}
Thanks in advance :)
Edit:
Might be worth adding that the code where this snippet gets called looks something like this, with very small variations;
Console.WriteLine($"1. Add a user\n2. Remove a user \n3. See user info \n \n9. Cancel");
Program.checkForEsc();
int response2 = CheckIfResponseInt(Console.ReadLine());
You need to write your own line editor instead of using Readline. It's described in the link below. You may need to add support for other control keys such as backspace or add a check that only numeric keys are being pressed.
Listen on ESC while reading Console line

How to get rid of the ReadKey() que if a person where to spam a key?

My console application uses ReadKeys thruout the code; if a person where to say click a key when an ReadKey is not active, it would to my speculation be added to a que of sorts, so when the code gets to the readKey it instantly goes on the previus keypress and continues as if that key was pressed.
Is there a way to fix this?
Edit: To clarify, axidental keypresses affect the ReadKeys that have not yet been run
You can check if there is any key available in the input stream and then read the next key.
A very good solution can be found here: Clear Console Buffer
Another similar solution here: https://newbedev.com/csharp-c-console-clear-input-buffer-code-example
// while there are characters in the input stream
while(Console.KeyAvailable)
{
// read them and ignore them
Console.ReadKey(false); // true = hide input
}
// Now read properly the next available character
Console.ReadKey();

Console.Write makes Sound

today I came across something that actually scared me when it happened.
I was reading out a file as an Byte-Array and printed each byte out converted as a char like the following:
byte[] bytes = System.IO.File.ReadAllBytes(fileName);
foreach(byte bt in bytes)
{
Console.Write((char)bt + " ");
}
The thing is now, that printing the converted values out to Console actually made a sound in my headset and my general audio-output..
When I then clicked into the console to stop the execution, after a few seconds there was a Windows-Notification-Sound like when you get an update or something like that.
My question now is why this is happening?
Also note that I tested the File.ReadAllBytes using a mp4-file first and then with a .zip. With a plain .txt-file it doesnt seem to work.
Also I am using Windows 10.
Thanks to the comments I was able to figure out that a beeping-character was actually called out, which caused Windows 10 to do basically infinite beeping-sounds.
I checked for the Hex-Value of 0x07 now before emitting the sound, and it turned out that, after setting a breakpoint, it actually is in the byte-array and when printed it made the sound.
Thanks everyone, I am not cursed after all ;) :)
PS:
I used the german Wiki-Page to get the hex-value:
https://de.wikipedia.org/wiki/Steuerzeichen
On the English one I couldnt find it

Create CMD-like arguments for SendKeys

I'm currently working on a Program, that presses buttons for me. I'm working on WPF but I already finished my design in XAML, now I need some C# code.
I have a TextBox that should handle all the SendKeys input. I want to extend it's functionality by providing some CMD-like arguments. The problem is, I don't know how. ;A; For example:
W{hold:500}{wait:100}{ENTER}
This is a example line that I'd enter in the textbox. I need 2 new functions, hold and wait.
The hold function presses and holds the previous key for the specified time (500 ms) and then releases the button.
The wait function waits the specified time (100ms).
I know I could somehow manage to create this function but it would end up being not user editable. That's why I need these arguments.
You're trying to 'parse' the text in the text box. The simplest way is to read each character in the text, one by one, and look for '{'. Once found, everything after that up until the '}' is the command. You can then do the same for that extracted command, splitting it at the ':' to get the parameters to the command. Everything not within a '{}' is then a literal key you send.
There are far more sophisticated ways of writing parsers, but for what it sounds like you are doing, the above would be a good first step to get you familiar with processing text.

Is there a better alternative to Console.ReadKey()?

Most programming books will tell you to use Console.ReadKey() to pause the console, but is there a better alternative?
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
You haven't actually told us what you wish to achieve.
If you wish to stop the output until the user chooses to continue, then you're not really going to get much better than just waiting for a key to be pressed using Console.ReadKey. If you just want to pause the output for a certain amount of time, you can use the Thread.Sleep method, which doesn't require any human invtervention.
How about Console.ReadLine() :)
system("pause");
does the same thing

Categories