Performing some action while waiting for a key to be pressed in .NET console applications

You can wait for the user to press some button in a .NET console application using the Console.ReadKey() method. That’s simple and easy to use, but occasionally you might want to perform some action while waiting for the user to press a key.

The KeyAvailable property of the Console object helps you achieve just that.

Read more of this post

Advertisement

How to hide the text entered in a .NET console application

You’ve probably encountered console applications that ask for a password. It’s very likely that the password will stay hidden otherwise other people viewing your screen can easily read it.

This short post will present a possible solution on how to achieve a hidden string input in a .NET console application.

Read more of this post

How to hide the key pressed in a .NET console application

You probably know how to read the key pressed by the user in a .NET console application by the Console.ReadKey() method:

ConsoleKeyInfo pressedKey = Console.ReadKey();
Console.WriteLine("You have pressed: {0}", pressedKey.Key);

Read more of this post

How to hide the text entered in a .NET console application

You’ve probably encountered console applications that ask for a password. It’s very likely that the password will stay hidden otherwise other people viewing your screen can easily read it.

This short post will present a possible solution on how to achieve a hidden string input in a .NET console application.

Read more of this post

Performing some action while waiting for a key to be pressed in .NET console applications

You can wait for the user to press some button in a .NET console application using the Console.ReadKey() method. That’s simple and easy to use, but occasionally you might want to perform some action while waiting for the user to press a key.

The KeyAvailable property of the Console object helps you achieve just that.

Read more of this post

How to redirect standard output for a .NET console application

Normally a .NET console application writes its messages directly to the console so that the user can view them. This is done with one of the “Write” methods like Console.WriteLine(some message) where you can prompt the user for some input.

However, this is not the only option you have. The standard output channel can be overridden.

Read more of this post

How to warn the user if either CapsLock or NumLock are pressed in a .NET console application

You’ve probably come across applications that warn you if the CapsLock button is pressed when typing a password. It’s a clever way to warn the user as passwords are normally not shown on the screen in plain text so it’s difficult to see what you’re typing. Similarly the NumLock button affects the behaviour of various keys on your keyboard.

Detecting whether those two special buttons are pressed is very simple in .NET console applications.

Read more of this post

Extracting information about key pressed in .NET console applications

Console applications let you extract the key(s) pressed by the user using the Console.ReadKey() method. It returns an object of type ConsoleKeyInfo which includes a number of useful properties.

Read more of this post

How to redirect standard error output for a .NET console application

Normally a .NET console application writes its exception messages directly to the console so that the user can view them.

Here’s an example:

static void Main(string[] args)
{
	RunStandardErrorRedirectExample();			
	Console.ReadKey();
}

private static void RunStandardErrorRedirectExample()
{
	try
	{
		double res = Divide(100, 0);
		Console.WriteLine(res);
	}
	catch (Exception ex)
	{
		using (TextWriter errorWriter = Console.Error)
		{
			errorWriter.WriteLine(ex.Message);
		}
	}
}

private static double Divide(int divideWhat, int divideBy)
{
	return divideWhat / divideBy;
}

You’ll get “Attempted to divide by 0” in the console.

However, this is not the only option you have. The standard error output channel can be overridden.

Read more of this post

Subscribing to cancel key press events in a .NET console application

Users can stop and close a console application by any of the following methods:

  • Pressing the ‘x’ button in the top right hand corner
  • Pressing ctrl+c
  • Pressing ctrl+break

Occasionally you might want to check whether the user has pressed either ctrl+c or ctrl+break. The following code sample will show you how to do that.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: