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