Updated Computer Name App

Way back in February 2016 I put together a small C# and XAML application which displayed the users computer hostname, IP address and Active Directory domain. This was to replace a tool used internally at work written in an older code (which still checked out). I had for ages talked about adding a little icon which would also indicate whether the user had connectivity to a specified endpoint or not. This week I decided it was time to get this feature added so I could tick it off my list of things to do… well at least till I decide to add the next feature!

For reference here is the original blog post and a link to the new GitHub repo –

https://www.bytesizedalex.com/c-and-xaml-app-to-show-computer-name-domain-and-ip/

https://github.com/bytesizedalex/ComputerName

I should remind everyone I’m in no way a programmer/developer so this code could probably be improved vastly – if you want to share how I’m always open to suggestions and learning :)

The examples below are just off a virtual machine I spun up quickly hence the random hostname and workgroup, but they are sufficient to give you an idea of what it looks like. If you click the ‘Copy’ button the on screen host details are placed into the clipboard so you can paste the information into another tool or record.

Example of the app with a status of green meaning connection attempt was successful.

ComputerName Status Green

Example of the app with a status of red meaning connection attempt was not successful.

ComputerName Status Red

 

C# Code

using System.Windows;
using System.Net;
using System.Linq;

namespace ComputerName
{
    public partial class MainWindow : Window
    {
        IPAddress[] addresses;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void StatusIndicator_Initialized(object sender, System.EventArgs e)
        {          
            try
            {
                using (var client = new WebClient())
                using (var stream = client.OpenRead("https://www.google.co.uk")) /// Enter the URL you wish to check for connectivity. In this instance Google is used as an example
                {
                    StatusIndicator.Fill = System.Windows.Media.Brushes.Green;
                }
            }
            catch
            {
                StatusIndicator.Fill = System.Windows.Media.Brushes.Red;
            }
        }

        private void MachineName_Initialized(object sender, System.EventArgs e)
        {
            MachineName.Text = (System.Environment.MachineName);
        }

        private void Domain_Initialized(object sender, System.EventArgs e)
        {
            Domain.Text = (System.Environment.UserDomainName);
        }

        private void IPs_Initialized(object sender, System.EventArgs e)
        {
            addresses = Dns.GetHostAddresses(Dns.GetHostName()).Where(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToArray();

            foreach (object address in addresses)
            {
                IPs.Items.Add(address);
            }
        }
        private void Copy_Click(object sender, RoutedEventArgs e)
        {
            string copiedDetails = MachineName.Text + "\r\n" + Domain.Text + "\r\n";

            foreach (var ipAddress in addresses)
            {
                copiedDetails += ipAddress.ToString() + "\r\n";
            }

            Clipboard.SetText(copiedDetails);
        }
    }
}

XAML Code

<Window x:Name="Computer_Name" x:Class="ComputerName.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ComputerName"
        mc:Ignorable="d"
        Title="Computer Name" Height="266.345" Width="310.483" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="ComputerName" HorizontalAlignment="Left" Margin="10,21,0,0" TextWrapping="Wrap" Text="Computer Name" VerticalAlignment="Top"/>
        <TextBox x:Name="MachineName" HorizontalAlignment="Left" Height="23" Margin="127,21,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Initialized="MachineName_Initialized" IsReadOnly="True"/>
        <TextBlock x:Name="DomainName" HorizontalAlignment="Left" Margin="10,71,0,0" TextWrapping="NoWrap" Text="Domain Name" VerticalAlignment="Top"/>
        <TextBox x:Name="Domain" HorizontalAlignment="Left" Height="23" Margin="127,71,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Initialized="Domain_Initialized" IsReadOnly="True"/>
        <TextBlock x:Name="IPAddresses" HorizontalAlignment="Left" Margin="10,120,0,0" TextWrapping="Wrap" Text="IP Addresses" VerticalAlignment="Top"/>
        <ListBox x:Name="IPs" HorizontalAlignment="Left" Height="84" Margin="127,120,0,0" VerticalAlignment="Top" Width="120" Initialized="IPs_Initialized"/>
        <Button x:Name="Copy" Content="Copy" HorizontalAlignment="Left" Margin="10,180,0,0" VerticalAlignment="Top" Width="74" ToolTip="Click to copy details" Click="Copy_Click" RenderTransformOrigin="0.5,1.5"/>
        <Ellipse x:Name="StatusIndicator" HorizontalAlignment="Left" Height="18" Margin="267,202,0,0" Stroke="Black" VerticalAlignment="Top" Width="20" Initialized="StatusIndicator_Initialized" RenderTransformOrigin="-0.15,-0.333" StrokeThickness="0"/>
    </Grid>
</Window>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.