C# and XAML App to Show Computer Name Domain and IP

I recently put together a little application written in C# and XAML (eXtensible Application Markup Language) which provides a user with their computer name, domain and IP addresses.

At work we have an older tool which does something similar but I wanted to get to grips with C’# and this seemed like a good place to start. Now I want you to understand I am in no way a developer so I wouldn’t be surprised if I have mistakes or areas I could improve on. If you are a developer or have useful feedback I welcome it. I’m lucky enough to have an MSDN subscription through work so I used Visual Studio 2015 Enterprise edition to create this project.

Going forward I’d like to enhance this application with some extra features –

  • Indicator circle which sets its colour –
    • Green – connected to the users primary Active Directory network and able to ping the domain name
    • Amber – connected to a network and able to ping an Internet resource
    • Red – no network connectivity
  • Additional network information, e.g which adapters IP addresses are bound to
  • Additional tabs
    • Currently logged on user details
    • System information such as free drive space

I’m sure other ideas will pop into my mind but for the moment I have the above to work on.

Here is an example screenshot of the application –

Computer Name Screenshot

 

Clicking the copy button will add the field values to the clipboard, if you then paste those values into say Notepad you will get something similar to this –

Copy Output Example

 

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="248.345" Width="290.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"/>

    </Grid>
</Window>

 

 

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 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);
         }

    }

}

 

I’m quite happy with this little app, there is certainly lots I can do to enhance and improve it. While learning to be a C# developer isn’t exactly my goal it is nice to be able to put something together and learn some new skills.

1 thought on “C# and XAML App to Show Computer Name Domain and IP”

Leave a Reply

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