IT-info > C#.NET

C#.NET

C#.NET programmering (Code reference & info)

Hello World

The customary first program that prints Hello World to the screen. The compiled .exe file is run in Command Prompt in Windows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

Read input

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Type your name and press Enter:");
            string UserName = Console.ReadLine();
            Console.WriteLine("\n", UserName);
        }
    }
}

Get host name (local computer name)

Note: Though both System.Environment.MachineName and the Dns.GetHostName() function can be used to print the host name (local computer name) to the screen, it makes more sense to use the Dns.GetHostName() function in a networking context, i.e client / server applications.

Get the hostname (local computer name) with System.Environment.MachineName

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string localComputerName = System.Environment.MachineName;
            Console.WriteLine(localComputerName);
        }
    }
}

Get the hostname (local computer name) with Dns.GetHostName()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net; // Needed by Dns.GetHostName function

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            string localComputerName = Dns.GetHostName();
            Console.WriteLine(localComputerName);
        }
    }
}

Reading a file with WebClient and Streamreader

Reads the text content of a file and print it to the screen. I.e if you specify a HTML page as URL, the source code of the file will be printed to the screen.

using System;
using System.Net;
using System.IO;

namespace NetworkFileReader
{
    class Program
    {
        static void Main(string[] args)
        {
            String uriString = "URL";
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            // Download page data.
            Console.WriteLine("Accessing {0} ...", uriString);
            // Open a stream to point to the data stream coming from the Web resource.
            Stream myStream = myWebClient.OpenRead(uriString);

            Console.WriteLine("\nDisplaying Data :\n");
            StreamReader sr = new StreamReader(myStream);
            Console.WriteLine(sr.ReadToEnd());

            // Close the stream.
            myStream.Close();
        }
    }
}

Declare and use variables

string aString = "world";
Console.WriteLine($"Hello {aString}");

string aString = "morning";
string anotherString = "world";
Console.WriteLine("Good {0} {1}", aString, anotherString);

string aString = "world";
Console.WriteLine("Hello " + aString);

Info and source in external files

Five in a row  (WPF)
Five in a row (2)  (WPF)
Source code reference (Dll import, Command line arguments, System.IO, Shell32)
Get info from network adapters
NetworkInterface Speed Property