Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 135,980 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,303 people online right now. Registration is fast and FREE... Join Now!




Redirecting StdOut to a StringBuilder Array

 
Reply to this topicStart new topic

Redirecting StdOut to a StringBuilder Array

itpro4470
12 Dec, 2007 - 07:24 PM
Post #1

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 118



Thanked: 1 times
Dream Kudos: 25
My Contributions
This is a piece of an IP scanner I am writing. I couldn't find anything in System.* to find netbios names so I decided to go with the process class input the command nbtstat -A <ipaddress> the reditect the standard output to an array of StringBuilder objects. this is where my problem lies with trying to populate my StringBuilder Array I am getting a NullReferenceException on the first iteration of the population of the String builder array. This is my test code for my solution

CODE

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string address = "192.168.1.1";
            StringBuilder netBiosName = new StringBuilder();
            //Create the process object
            Process netName = new Process();

            //set the process command and the arguments
            ProcessStartInfo startInfo = new ProcessStartInfo("nbtstat", "-A " + address.ToString());

            //set UseShellExecute to false and RedirectStandardOutput to true
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;

            //set the start info for the process
            netName.StartInfo = startInfo;

            //start the process
            netName.Start();

            //create a streamreader to read the output
            StreamReader reader = netName.StandardOutput;

            //create a stringbuilder to hold the data
            StringBuilder[] data = new StringBuilder[255];


            int century = 0;
            //append the standard output to the string builder
            while (!reader.EndOfStream)
            {
                **********NullReferenceException********
                data[century].Append(reader.ReadLine());
                century++;
            }
            century = 0;

            //close the reader, stream writer, buffer and filestream
            reader.Close();

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i].ToString().Contains("<20>"))
                {
                    netBiosName.Append(data[i].ToString());
                }
            }
            Console.WriteLine(netBiosName);
        }
    }
}




please help!!!

Wow what a friggin n00bish mistake

just had to change
data[century].Append(reader.ReadLine());
to
data[century].new StringBuilder(reader.ReadLine();

and it works great
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Redirecting StdOut To A StringBuilder Array
22 Dec, 2007 - 06:00 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Glad you found the solution as I was going to mention that your StringBuilder object was never initialized.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 10:31AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month