Skip to main content

The Power and Prowess of Console Applications and Enhancing it with .NET Framework 2.0

The Power and Prowess of Console Applications and Enhancing it with .NET Framework 2.0

To start learning a new programming language, following the C++ book style, the first and foremost application that we write is

cout << "Hello World";

We have been religiously inheriting this custom through Java and to C# via the essence of Console Applications.

java:
System.out.println ("Hello World");

C#:
System.Console.WriteLine ("Hello World");

A simpler console application lays the greater foundation for a powerful and rewarding journey through the programming language. Let us in brief see about the essence of these console applications and how .NET Framework BCL richly complements console applications.

The Inspiration

This article was actually inspired from a discussion board question where a beginner had just vehemently violated the message board netiquette about a sample code snippet which an expert had provided with great magnanimity. Fuelling it, he/she had also scrapped base about console applications. I just thought I would take this opportunity to initiate the efforts in clearing the myth clouding about the console applications.

Simpler and Stronger

  1. Console applications are a stronger support to even a full-fledged application frameworks. For example consider an application like 'Windows Service' project in C#. By default, you can not run a Windows Service project but you can only attach to the running process to debug the same. But there is an easy workaround using the Visual Studio IDE. With the other classes that are used in the same way, the launching class (Service.cs) can be excluded from the project while debugging and a sample (StartupConsole.cs) can be used. This can emulate threads from within a sample Console application facilitating easier peek of the startup configuration of the Windows service.
  2. Console applications are a very good applications to provide code snippets for articles since they are easier to visualize, compile for the casual article viewer also.
  3. Console applications are lighter and supported by the heart library of the framework. .NET Framework supports Console right in System namespace (rooted to the core) and Java supports in its java.lang.
  4. There is also a good business potential of console applications. There are still a lot of retail shops who prefer a commandline console based data entry approach for billing and other things. We can use the richness of Console applications there.


Simpler and Sweet Extensions to Console in .NET Framework 2.0

The console applications have grown more powerful at least with respect to .NET Framework 2.0. I first thought I would provide a Class documentation for the same in http://www.dotnetspider.com/ but then I chose to include it as part of this article context for better reach.

A few of the new namespaces that add color and brightness to to the otherwise dark console are:

  1. BackgroundColor
  2. ConsoleColor enumeration
  3. CapsLock (To detect whether CAPSLock is on or off in just a jiffy)
  4. NumberLock (To detect whether NumberLock is on or off in just a jiffy)
  5. Clear (To clear the console display. Our class CLS command of DOS. No more P/Invoke :))
  6. A host of other Window manipulation utilities like SetWindowPosition, SetWindowSize
  7. ReadKey -- Equivalent to provide 'Press any key to continue in C#' without RETURN.

However, for older .NET Framework versions, I have a workaround in my other article here: http://www.codeproject.com/useritems/PressAnyKeyToContinue.asp

P.S.: A significant amount of them are also available in .NET Framework 1.1 albeit you have to delve into P/Invoke Library for the same. However, I would suggest ManiB's article on CodeProject which hides this P/Invoke management from your core application logic. Check it out here: http://www.codeproject.com/cs/library/csconsolelibrary.asp

This article is also part of my other article in Dotnetspider. You can check it out here.

Comments

Popular posts from this blog

Google NCR

Google NCRMost of you would be knowing about this trick in navigating to Google generic website. But I just came to know only a few days before and hence thought I would share the information with others too.
Whenever you type http://www.google.com/, Google automatically tries to redirect you to your country-specific website. For me, it always redirects to http://www.google.co.in/ (Google India). But when we specifically want to navigate to Google Generic website for some reason, there are two ways to accomplish the same.Click the 'Go to Google.com' link at the bottom of the webpage that is displayed from http://www.google.co.in/To prevent this redundant click, you can opt to type http://www.google.com/ncr/ which would prevent the redirect from happening. The NCR stands for No Country Redirect. There is a brief discussion about this NCR over this forum.You may also like to check out Google help documentation regarding the country redirections from here.

Elegant 'MailTo' Replacement

Elegant 'MailTo' Replacement

MailTo tags have become a bane to all websites since spammers run spam robots which crawl the web to capture the email addresses, add to their catalogs and send spams to them. I just thought of sharing with others a simple JavaScript function which would trigger open the default email client instead of having mailto: embedded in the HTML Page.

Just have the following function included in your webpage

<script language='Javascript'>
function WriteMailTo(user,domain)
{
location.replace ('mailto:'+user+'@'+domain);
}
</script>

And whereever you want to invoke the mailto, just called WriteMailTo('user','example.com') and the user's default email client should be launching (if configured) without any hassles.

Contacts import utility from Web2Project to Addressbook

I have been using Web2Project for planning my personal and professional tasks for a few years now. Now I have bifurcated the tools into the following:

Project PierAddress Book The only issue I had was to import my addressbook from Web2Project to AB. PP is a different animal. It does not serve as a comprehensive candidate for addressbook and hence I needed this bifurcation. 
I wrote up a following simple script to import the contacts to AB. The pre-requisite for this script is to hop on to PhpMyAdmin, export the contacts as a PHP array and then  modify the path in our import script to whatever you downloaded from PhpMyAdmin.
Our import script would explode the entries as SQL scripts into the browser window. Copy them and execute into PhpMyAdmin AB database to have the contacts imported.

Script URL: https://gist.github.com/lavanyadeepak/5177500#file-import-php