Sunday, December 16, 2012

NET VIEW and C#

Part I: TERMS

What is NET VIEW command?

It's a Windows shell command that display a list of computers visible on the network. To use the command just press the winkey, type cmd.exe, hit enter, and on the command prompt just type NET VIEW, see below for the result.



What is the Standard Output?

Standard output is the stream where a program writes its output data (e.g. screen, file, etc...).


Part II: THE APPLICATION

Objective

Build a C#.NET Console Application that is able to read the standard output stream of the NET VIEW command to show a list of connected computers in a network.

Project

1. Click on .
2. Choose  from the start menu.
3. On Visual Studio choose File, New, and then Project.
4. The New Project dialog will appear, name the project as 'NetView', then hit .
5. Navigate to the Solution Explorer, Right click the NetView project, select Add, and then choose Class.
6. The Add New Item dialog will show, name it as PC, then click 
7. Enter the code below for the PC.cs...

namespace NetView
{
    public class PC
    {
        public string ServerName { get; set; }
    }
}

8. Enter the code below for the Program.cs...

using System;
using System.Collections.Generic;
using System.Text;

namespace NetView
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var pcs = DisplayComputers();
                if (pcs != null)
                {
                    foreach (var pc in pcs)
                        Console.WriteLine(pc.ServerName);
                }
                else
                    Console.WriteLine("Err Msg: There Are No Entries in the List");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadKey();
        }

        /// <summary>
        /// This method will display the list of computers in the current workgroup.
        /// </summary>
        /// <returns>List of computers</returns>
        public static List<PC> DisplayComputers()
        {
            // Create a child process
            System.Diagnostics.Process process = new System.Diagnostics.Process();

            // Starts a new instance of the Windows command interpreter
            process.StartInfo.FileName = "cmd.exe";

            // Carries out the NET VIEW command and then terminates
            process.StartInfo.Arguments = "/c net view";
            process.StartInfo.CreateNoWindow = false;

            // Redirect the output stream of the child process.
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;

            // Start!
            process.Start();

            List<PC> pcs = new List<PC>();
            StringBuilder pc;

            // Read the output stream
            while (!process.StandardOutput.EndOfStream)
            {
                // This is the current stream data
                string line = process.StandardOutput.ReadLine();

                // ref: http://support.microsoft.com/kb/103013
                if (line == "There are no entries in the list.")
                    return null;

                // Find/Parse the pc name
                if (line != string.Empty)
                {
                    if (line[0] == '\\' && line[1] == '\\')
                    {
                        pc = new StringBuilder();

                        for (int y = 2; y < line.Length; y++)
                        {
                            if (line[y] == ' ')
                                break;
                            else
                                pc.Append(line[y]);
                        }

                        // Add the current PC to the PC list
                        pcs.Add(new PC { ServerName = pc.ToString() });
                    }
                }
            }

            // Wait indefinitely for the associated process to exit
            process.WaitForExit();

            // Frees all the resources
            process.Close();

            return pcs;
        }
    }
}


Output