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 […]

How to get website header information in C#

With the following function you can easily retrieve a list of website headers in C#. The function needs only the url of a webpage to collect the data. If the function is successfully executed, you’ll get a Dictionary, typed as <string, string>, as return value. This Dictionary<string, string> contains all the headers of the webpage, whose url you passed by at function call time.

//Don’t forget to set the using-directive ;)
using System.Net;

//…

public Dictionary getHeaders(string url)
{
//Dictionary which shall contain the header information
Dictionary header = new Dictionary();

//Create webrequest
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

//Create response object
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//Read all headers from the response
foreach (string headerItem in response.Headers)
{
//Add header to the dictionary
[…]