Getting Info From Command Prompt

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Hello all:

I have yet another question to ask the gurus of C#. I am trying to get
a list of all the connected computers on our domain. The only way I
know how is by going to the command prompt and typing net view. Is
there a way to use this list in a program? Is there a better way to
accomplish this task?

Thanks,

- John -
 
If you choose to use net view and parse the output - you can use process
class to start "net view" command and redirect the output to a variable

or you can use WMI to get that information

HTH
John
 
John,

I know of a couple of ways that you can get this information. One is to use
the System.DirectoryServices. You can use this class enumerate through the
computers in a domain. The other way is to use the System.Process class and
use NET VIEW and save the output in a string. I have posted a sample of each
below to demonstrate.

I hope this helps
-------------

//System.DirectoryServices
string path = "WinNT://<domain>";
string username = @"<domain>\<username>";
string password = "<password>";
DirectoryEntry domain = new DirectoryEntry(path, username, password);
DirectoryEntries computers = domain.Children;
computers.SchemaFilter.Add("Computer");
foreach(DirectoryEntry computer in computers)
{
Console.WriteLine(computer.Name);
}

//System.Process
ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName = "NET";
pi.Arguments = "VIEW";
pi.UseShellExecute = false;
pi.RedirectStandardOutput = true;
pi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = pi;
p.Start();
string s = p.StandardOutput.ReadToEnd();
Console.WriteLine(s);
 
Back
Top