How to solve representation/encoding errors in C# Winforms applications

I know the title of this post hurts, but I wasn’t able to describe the problem with fewer words. But let us get to the point. As part of the development of my cloud downloader I encountered (among much others) the following problem. Korean characters were displayed as blank boxes, even though they were shown in the source code editor correctly. Those of you, who wonder now,  from where you can get the Korean characters on your keyboard, should just stop by Google Translate. (The screenshot below illustrates the problem once again. Please click to enlarge the image.)
Koreanische Zeichen falsche Darstellung
On the Internet I found only very few solutions. And almost all were, in my opinion, rather suboptimal. In most cases the following solution was proposed:
It is recommended that you call the SetCompatibleTextRenderingDefault() method with the parameter true. In […]

How to add DLL to Visual Studio projects

Some days ago I received an email, in which I was asked, how to embed/include .dll-files to into Visual Studio projects. So here comes the answer to the question: How to add dlls to an ongoing Visual Studio project?
The solution to the problem is closer than some of you might think. First you right-click on the “references”-label in the Solution Explorer and click in the context menu on “Add reference” . (Fig. 1)
Now click on the “Browse” button, select the desired (. NET compatible) DLL and confirm your selection by clicking on the “Ok” button. (Fig. 2)
In the last step you can add the appropriate using directives in your source code for an easier access to the methods of the obtaining DLL. (Fig. 3)
Visual Studio - Verweise  Dlls auswählen

How to save configuration settings in C#

How to save settings really quick and easy in C # applications? I’m sure this question has been asked certainly by everyone once during his career as C# programmer. And did you know? The answer is quite simple. The easiest way to answer this question is to use the  AppSettings from ConfigurationManager class of the .Net-Framework.
The. Net Framework brings its own managers to save and load settings with it. The properties are stored in the common XML format. And so you can see that dealing with the AppSettings is really a no-brainer , I have written two small functions for saving, changing and reading preferences.

public string getAppSetting(string key)
{
//Load the appsettings
Configuration config = ConfigurationManager.OpenExeConfiguration(
System.Reflection.Assembly.GetExecutingAssembly().Location);
[…]

C# Random.Next(int, int) – how to define the bounds correctly

The devil is in the detail. Recently I wanted to create random numbers between 0 and 1 in C#. The Visual Studio tells us that we have to use Random.Next (int, int) method, whereby the first integer defines the lower limit and the second integer defines the upper limit. No more and no less. So based on Visual Studio’s description the following function should work:

Random rnd = new Random();
int random = rnd.Next(0,1);

Wrong! So you’ll get only one thing – in fact zeros. So I had to dip into the MSDN library to find a solution. There is explained, on the contrary to IntelliSense from Visual Studio, that the lower bound is inclusive and the upper bound is exclusive.

Parameters

minValue
Type: System.Int32
The inclusive lower bound of the random number returned.

maxValue
Type: System.Int32
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.

In plain language this means, to […]