AboutForm.cs

 //
// Copyright © 2006 Herbert N Swearengen III (hswear3@swbell.net)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   - Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   - Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
// OF SUCH DAMAGE.
//

using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;
using System;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;

namespace StartupManager
{
   
/// <summary>
/// Generic About Box for use with my projects. Provides basic system information as well as
/// system up time and the current date and time. The header is a panel which my contain a
/// background image or color. A picture box (48x48 pixels) can display a custom image.
///
/// There are two buttons on the form, "Microsoft System Information," which starts msinfo32.exe
/// and "OK," which dismisses the form. Typing Escape on the keyboard will also dismiss the form.
///
/// There are two link labels. The first opens a file called "EULA.pdf" using the Adobe Reader that
/// is installed on the computer. The "EULA.pdf" file must reside in the application folder. The other
/// link label initiates an email to me. This is normally not visible for non-commercial applications.
/// the application folder.
/// </summary>
/// <remarks>The application will produce an error dialog if msinfo32.exe or EULA.pdf is not found.</remarks>
/// <copyright>Copyright © 2006 Herbert N Swearengen III</copyright>
public partial class AboutForm : Form
{
        public AboutForm()
        {
            InitializeComponent();
        }

        #region Form Events

        /// <summary>
        /// When user presses the escape key close this form.
        /// </summary>
        private void AboutForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                e.Handled = true;
                this.Close();
            }
        }

        /// <summary>
        /// Form Load Event - assign form level variables, fill out labels on form
        /// </summary>
        private void AboutForm_Load(System.Object sender, System.EventArgs e)
        {
            try
            {

                //  enable timer
                timerTickCount.Enabled = true;

                //  get major and minor versions and set version on form
                labelVersion.Text = AssemblyTitle + " Version " + AssemblyVersion;

                //  set title on form
                this.Text = AssemblyTitle;

                //  fill in application information
                labelTitle.Text = AssemblyTitle;
                labelCopyright.Text = AssemblyCopyright;
                labelDescription.Text = AssemblyDescription;

                //  fill in user information
                labelUser.Text = GetUserCustomerName();
                labelOrganization.Text = GetUserCustomerOrganization();

                //  fill in company information
                labelCompany.Text = AssemblyTitle + " is a product of " + AssemblyCompany;
                linkLabelSupport.Text = "Support is available from: hswear3@swbell.net";

                //  fill in system information
                labelOSDescription.Text = System.Environment.OSVersion.ToString();
                labelProductVersion.Text = "Version " + System.Environment.OSVersion.Version.ToString();
               
                if (!String.IsNullOrEmpty(Environment.OSVersion.ServicePack.ToString()))
                {
                    labelProductVersion.Text = labelProductVersion.Text + " " + Environment.OSVersion.ServicePack.ToString();
                }
                labelFramework.Text = "NET Framework" + " Version " + GetFrameworkShortVersion();
                if (!String.IsNullOrEmpty(GetFrameworkServicePack()) | GetFrameworkServicePack() == "0")
                {
                    labelFramework.Text = labelFramework.Text + " Service Pack " + GetFrameworkServicePack();
                }

            }
            catch (NullReferenceException ex)
            {
                //  This will trap null references.
                MessageBox.Show(ex.Message.ToString(), AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #endregion

        #region Link Labels

        /// <summary>
        /// Display EULA if user clicks on link label. Note that this is a PDF file and an
        /// error will occur if Adobe Reader is not installed on the user's computer.
        /// </summary>
        private void linkLabelEULA_LinkClicked(System.Object sender, LinkLabelLinkClickedEventArgs e)
        {

            if (File.Exists(Application.StartupPath + @"\EULA.pdf"))
            {
                try
                {
                    //  Now display EULA
                    ProcessStartInfo startInfo = new ProcessStartInfo(Application.StartupPath + @"\EULA.pdf");
                    startInfo.WindowStyle = ProcessWindowStyle.Normal;
                    Process.Start(startInfo);
                }
                catch (Win32Exception)
                {
                    //  cannot find Adobe Reader.
                    MessageBox.Show("The EULA.pdf file cannot be found, or Adobe Reader is not installed.",
                    AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                //  cannot find file
                MessageBox.Show("The EULA.pdf file cannot be found.",
                    AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        /// <summary>
        /// Invoke an email message for support. If no email application is installed or running
        /// on the user's computer, then an error message will be displayed.
        /// </summary>
        private void linkLabelSupport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

            try
            {
                //  Now send email
                ProcessStartInfo startInfo = new ProcessStartInfo("mailto:hswear3@swbell.net");
                startInfo.WindowStyle = ProcessWindowStyle.Normal;
                Process.Start(startInfo);
            }
            catch (Win32Exception)
            {
                //  cannot find file
                MessageBox.Show("An email application cannot be found.", AssemblyTitle,
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        #endregion

        #region Get NET Framework Information

        /// <summary>
        /// Framework short version, ie: 2.0
        /// </summary>
        /// <returns> Returns a string with the version number, if there is an error an
        /// empty string is returned. </returns>
        private static string GetFrameworkShortVersion()
        {

            return Environment.Version.ToString().Substring(0, 3);

        }

        /// <summary>
        /// A special section of the registry has to be querried to find out the service pack
        /// of the .NET Framework. A different location was used for 1.0 and 1.1, but since this
        /// application only runs on version 2.0, we won't worry about that.
        /// </summary>
        /// <returns> A string containing the version number, for example: "2.0" </returns>
        private static string GetFrameworkServicePack()
        {
            string frameworkMajorVersion = Environment.Version.Major.ToString();
            string frameworkMinorVersion = Environment.Version.Minor.ToString();
            string frameworkVersion = "v" + frameworkMajorVersion + "." + frameworkMinorVersion
                + "." + Environment.Version.Build.ToString();
            RegistryKey rk;
            string temp;

            try
            {
                //  try each registry key to determine the version, build, and service pack
                if (frameworkMajorVersion == "2" & frameworkMinorVersion == "0")
                {
                    rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP"
                        + frameworkVersion, false);
                    temp = rk.GetValue("SP").ToString();
                    if (temp != "0")
                    {
                        return temp;
                    }
                    else
                    {
                        return String.Empty;
                    }

                }
                else
                {
                    return String.Empty;
                }
            }
            catch (Win32Exception)
            {
                return String.Empty;
            }

        }

        #endregion

        #region Get User Information

        /// <summary>
        /// Query registry for information.
        /// </summary>
        /// <returns> Returns customer name as string. </returns>
        private static string GetUserCustomerOrganization()
        {
            Microsoft.Win32.RegistryKey rk;

            try
            {

                switch (Environment.OSVersion.Platform)
                {
                    case PlatformID.Win32Windows:

                        rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion", false);
                        return rk.GetValue("RegisteredOrganization").ToString();

                    case PlatformID.Win32NT:

                        rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion", false);
                        return rk.GetValue("RegisteredOrganization").ToString();

                    default:
                        return "Unknown";
                }

            }
            catch (Win32Exception)
            {
                return "Unknown";
            }

        }

        /// <summary>
        /// Query registry for information.
        /// </summary>
        /// <returns> Returns customer organizationn as string. </returns>
        private  static string GetUserCustomerName()
        {

            try
            {
                Microsoft.Win32.RegistryKey rk;

                switch (System.Environment.OSVersion.Platform)
                {
                    case PlatformID.Win32Windows:

                        rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion", false);
                        return rk.GetValue("RegisteredOwner").ToString();

                    case PlatformID.Win32NT:

                        rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion", false);
                        return rk.GetValue("RegisteredOwner").ToString();

                    default:
                        return "Unknown";
                }

            }
            catch (Win32Exception)
            {
                return "Unknown";
            }

        }

        #endregion

        #region Get OS Up Time

        /// <summary>
        /// This method gets the system up time by using the system clock. Most of this
        /// code is used to format the output of the system timer (in milliseconds) to
        /// a more useful format:  DD:HH:MM:SS.
        ///
        /// Note since the system timer rolls over every 47 days, this method will return
        /// an incorrect value on a system that has been up for more than 47 days.
        ///
        /// This method is called by the timer.
        /// </summary>
        /// <returns> A formatted string containing the system up time in the format DD:HH:MM:SS </returns>
        private static string GetOSUptime()
        {
            int days;
            int hours;
            int minutes;
            int seconds;
            int remainder;
            int ticks;
            string stringDays;
            string stringHours;
            string stringMinutes;
            string stringSeconds;

            //  initialize string variables
            stringDays = String.Empty;
            stringHours = String.Empty;
            stringMinutes = String.Empty;
            stringSeconds = String.Empty;

            try
            {
                Application.DoEvents();

                //  updates tick counter ticks
                ticks = System.Environment.TickCount;

                //  there are  86400000 milliseconds in one day, compute whole days and get remainder
                do
                {
                    days = (int)(System.Math.Floor((double)ticks / 86400000));
                    remainder = ticks % 86400000;
                }
                while (!(remainder <= 86400000));

                //  there are 3600000 milliseconds in one hour, compute whole hours and get remainder
                do
                {
                    hours = (int)(System.Math.Floor((double)remainder / 3600000));
                    remainder = remainder % 3600000;
                }
                while (!(remainder <= 3600000));

                //  there are 60000 milliseconds in one minute, compute whole minutes and get remainder
                do
                {
                    minutes = (int)(System.Math.Floor((double)remainder / 60000));
                    remainder = remainder % 60000;
                }
                while (!(remainder <= 60000));

                //  there are 1000 milliseconds in one second, compute whole seconds and get remainder
                do
                {
                    seconds = (int)(System.Math.Floor((double)remainder / 1000));
                    remainder = remainder % 1000;
                }
                while (!(remainder <= 1000));

                //  format days
                if (days == 0)
                {
                    stringDays = String.Empty;
                }
                else if (days.ToString().Trim().Length == 1)
                {
                    stringDays = " " + days.ToString().Trim() + ":";
                }
                else if (days.ToString().Trim().Length == 2)
                {
                    stringDays = days.ToString().Trim() + ":";
                }

                //  format hours
                if (hours == 0 & days == 0)
                {
                    stringHours = String.Empty;
                }
                else if (hours.ToString().Trim().Length == 1)
                {
                    stringHours = "0" + hours.ToString().Trim() + ":";
                }
                else if (hours.ToString().Trim().Length == 2)
                {
                    string transTemp9 = hours.ToString();
                    stringHours = transTemp9.Trim() + ":";
                }

                //  format minutes
                if (minutes == 0)
                {
                    stringMinutes = "00" + ":";
                }
                else if (minutes.ToString().Trim().Length == 1)
                {
                    stringMinutes = "0" + minutes.ToString().Trim() + ":";
                }
                else if (minutes.ToString().Trim().Length == 2)
                {
                    stringMinutes = minutes.ToString().Trim() + ":";
                }

                //  format seconds
                if (seconds == 0)
                {
                    stringSeconds = "00";
                }
                else if (seconds.ToString().Trim().Length == 1)
                {
                    stringSeconds = "0" + seconds.ToString().Trim();
                }
                else if (seconds.ToString().Trim().Length == 2)
                {
                    stringSeconds = seconds.ToString().Trim();
                }

                //  return time string
                return stringDays + stringHours + stringMinutes + stringSeconds;
            }
            catch (OverflowException)
            {
                return String.Empty;
            }

        }

        #endregion

        #region Timer Code

        /// <summary>
        /// This timer is used to update the display of system up time and the date and time
        /// displayed in the StatusStrip at the bottom of the form.
        /// </summary>
        private void timerTickCount_Tick(System.Object sender, System.EventArgs e)
        {
            //  updates status bar display
            labelDateTime.Text = System.DateTime.Now.ToLongDateString() + " "
                + System.DateTime.Now.ToLongTimeString();
            labelUpTime.Text = "Up Time: " + GetOSUptime();
        }


        #endregion

        #region Buttons

        /// <summary>
        /// This button will close the form if the form was called with ShowDialog
        /// </summary>
        private void buttonOK_Click(System.Object sender, System.EventArgs e)
        {

            DialogResult = DialogResult.OK;

        }

        /// <summary>
        /// The form load event does most of the work here. It fills out all of the labels.
        /// </summary>
        private void buttonSysInfo_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                //  Now run msinfo32
                ProcessStartInfo startInfo = new ProcessStartInfo("msinfo32.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Normal;
                Process.Start(startInfo);
            }
            catch (FileNotFoundException)
            {
                //  cannot find file
                MessageBox.Show("Microsoft System Informtion cannot be found.", AssemblyTitle,
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        #endregion

        #region Assembly Attribute Accessors

        /// <summary>
        /// Get Title from AssemblyInfo.cs
        /// </summary>
        public string AssemblyTitle
        {
            get
            {
                // Get all Title attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                // If there is at least one Title attribute
                if (attributes.Length > 0)
                {
                    // Select the first one
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    // If it is not an empty string, return it
                    if (!String.IsNullOrEmpty(titleAttribute.Title))
                        return titleAttribute.Title;
                }
                // If there was no Title attribute, or if the Title attribute was the empty string, return the .exe name
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        /// <summary>
        /// Get Version from AssemblyInfo.cs
        /// </summary>
        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        /// <summary>
        /// Get Description form AssemblyInfo.cs
        /// </summary>
        public string AssemblyDescription
        {
            get
            {
                // Get all Description attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                // If there aren't any Description attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Description attribute, return its value
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        /// <summary>
        /// Get Product Name from AssemblyInfo.cs
        /// </summary>
        public string AssemblyProduct
        {
            get
            {
                // Get all Product attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                // If there aren't any Product attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Product attribute, return its value
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        /// <summary>
        /// Get Copyright from AssemblyInfo.cs
        /// </summary>
        public string AssemblyCopyright
        {
            get
            {
                // Get all Copyright attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                // If there aren't any Copyright attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Copyright attribute, return its value
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        /// <summary>
        /// Get Company from AssemblyInfo.cs
        /// </summary>
        public string AssemblyCompany
        {
            get
            {
                // Get all Company attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                // If there aren't any Company attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Company attribute, return its value
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        #endregion
    }
}

Project Homepage: