Console Application Basic (edit)

  • JsonTextWriter
  • Console Formatting (Indent) (PadLeft)
  • Console Output/Input Encoding
  • NLog & Log4Net
  • Debug Mode
  • Unicode characters (Janpanese)

Oracle database: Unicode Database and Unicode Datatype

Ký tự hán tự (kanji character): 鄭 瑋萱

Supporting Multilingual Databases with Unicode (oracle.com)

Console Application | Microsoft Docs

Tutorial: Create a simple C# console app - Visual Studio | Microsoft Docs

Tutorial: Extend a simple C# console app - Visual Studio | Microsoft Docs

Perfect console application in .net Core: add unit tests – Michał Białecki Blog (michalbialecki.com) (.NET Core) (HAY HAY HAY)

Perfect console application in .net Core: set up dependency injection – Michał Białecki Blog (michalbialecki.com) (SimpleInjector) (Unit Testing) (Dependency Injection)

C#: Building a Useful, Extensible .NET Console Application Template for Development and Testing - CodeProject (HAY HAY HAY)

Some Best Practices for C# Application Development (Explained) - CodeProject (HAY HAY HAY)

Command line - Correct way to implement C# console application? - Stack Overflow (HAY HAY HAY)

C# Logging best practices in 2021 with examples and tools · Raygun Blog (HAY HAY HAY)

C# Console Application Examples (50+ C# Examples) – Programming, Pseudocode Example, C# Programming Example (csharp-console-examples.com)

Error messages should be written to stderr aka Console.Error, and normal output to stdout aka Console.Out. This is particularly important for "filter" type console apps whose output (stdout) can be piped to another process, e.g. in a batch file.

Generally if you encounter an error, write an error message to Console.Error and return a non-zero result. Or if it's an exception, just don't bother handling it.

To return a result code, you can either pass it as an argument to Environment.Exit, set the Environment.ExitCode property, or return a non-zero value from main.

For simple console apps I would:

  • have a helper class to parse the command line.

  • have a facade class that provides a testable API for the functionality implemented by your command line tool. Like most .NET APIs, this would normally throw an exception if an error occurs.

  • the main program simply uses the helper to parse the command line and calls the API passing the arguments passed from the command line. It optionally catches exceptions thrown from the API, logs them, writes a user-oriented error message to Console.Error and sets a non-zero return code.

But I wouln't consider this to be the one true way: there isn't really such a thing which is why you're unlikely to find the book you're looking for.

Debug mode

private void PrintFirstName()
{
string firstName = string.Empty;
#if DEBUG
firstName = GetFirstName();
#endif
Console.WriteLine(firstName);
}
[Conditional("DEBUG")]
private void PrintFirstName()
{
string firstName = GetFirstName();
Console.WriteLine(firstName);
}

try {
//Do something
}
catch(ex as Exception) {
logger.Error(ex);
throw;
}

Show me the code

Console.Title = typeof(Program).Name;

Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.UTF8;

// Set the Foreground color to blue
Console.ForegroundColor = ConsoleColor.Blue;

// Set the Background color to black
Console.ForegroundColor = ConsoleColor.Blue;

Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;

// Clear output screen
Console.Clear();

// Restore the original console colors.
Console.ResetColor();

public class ConsoleFormatting
{
    public static string Indent(int count)
    {
        return "".PadLeft(count);
    }
}

JsonWriter writer;

public Calculator()
{
    StreamWriter logFile = File.CreateText("calculatorlog.json");
    logFile.AutoFlush = true;
    writer = new JsonTextWriter(logFile);
    ....
}

Program.cs

ConsoleApplicationBase/Program.cs at master · TypecastException/ConsoleApplicationBase (github.com)

Console Command

ConsoleApplicationBase/ConsoleCommand.cs at master · TypecastException/ConsoleApplicationBase (github.com)

Console Formatting (HAY HAY HAY)

ConsoleApplicationBase/ConsoleFormatting.cs at master · TypecastException/ConsoleApplicationBase (github.com)

CLI

Please use the .net port of the apache commons cli API. This works great.

http://sourceforge.net/projects/dotnetcli/

And the original API for concepts and introduction

http://commons.apache.org/cli/

Unicode characters

C# Unicode (Japanese Characters) - Stack Overflow

There are two conditions that must be satisfied in order for this to work:

  1. The console's output encoding must be able to represent Japanese characters
  2. The console's font must be able to render them

Condition 1 should be fairly simple to deal with; just set System.Console.OutputEncoding to an appropriate Encoding, such as a UTF8Encoding. (Of course, this won't work on Windows 9x, since that doesn't really support encodings or Unicode. But you aren't using that, now, are you?)

Satisfying condition 2 is a bit more involved:

  1. First, an appropriate font must be installed on the user's system. If there aren't any installed yet, the user will have to install some, perhaps by:

    • Opening intl.cpl ("Regional and Language Options" in the Control Panel on Windows XP in English)
    • Going to the "Languages" tab
    • Enabling "Install files for East Asian languages"
    • Clicking "OK"
  2. Actually getting the console to use such a font seems to be fairly hairy; see the question: How to display japanese Kanji inside a cmd window under windows? for more about that.

c# - How to write Unicode characters to the console? - Stack Overflow

Besides Console.OutputEncoding = System.Text.Encoding.UTF8;

for some characters you need to install extra fonts (ie. Chinese).

In Windows 10 first go to Region & language settings and install support for required language: enter image description here

After that you can go to Command Prompt Proporties (or Defaults if you like) and choose some font that supports your language (like KaiTi in Chinese case): enter image description here

Now you are set to go: enter image description here

References

Console.OutputEncoding Property (System) | Microsoft Docs

Encoding.UTF8 Property (System.Text) | Microsoft Docs

Change Console Foreground And Background Color In C# (c-sharpcorner.com)

C# | How to change Foreground Color of Text in Console - GeeksforGeeks

C# Convert console output UTF-8 to display in textbox - special characters problems (microsoft.com)