Process proc = new Process();

proc.StartInfo.FileName = "net.exe";

proc.StartInfo.CreateNoWindow = true;

proc.StartInfo.Arguments = "view";

proc.StartInfo.RedirectStandardOutput = true;

proc.StartInfo.UseShellExecute = false;

proc.Start();

StreamReader sr = new StreamReader(proc.StandardOutput.BaseStream);

string line = "";

DataTable dtNames = new DataTable();

dtNames.Columns.Add(new DataColumn("Host Name"));

while ((line = sr.ReadLine()) != null)

{

if (line.StartsWith(@"\"))

{

DataRow dtNewRow = dtNames.NewRow();

dtNewRow[0] = line.Substring(2).TrimEnd();

dtNames.Rows.Add(dtNewRow);

}

}

sr.Close();

proc.WaitForExit();

dataGridView1.DataSource = dtNames;

Advertisements