2011-03-25 8 views
0

실험실에 10 대의 컴퓨터가 있습니다. 우리는 이 파일을 공유 할 수 있도록 LAN에 연결된 모든 컴퓨터를 설정합니다. 내 컴퓨터는 주 컴퓨터로 사용되며, 단지 모든 것을 얻고 싶습니다. IP 주소 메인 컴퓨터에 연결된 컴퓨터 (즉, 내 컴퓨터입니다) 및 해당 목록을 나열 내가 실험실에서 10 시스템의 IP 주소로 원하는, 내 코드클라이언트 컴퓨터의 IP 주소를 얻으십시오

System.Diagnostics.Process p = new System.Diagnostics.Process(); 
    p.StartInfo.FileName = "cmd "; 
    p.StartInfo.UseShellExecute = false; 
    p.StartInfo.Arguments = "/C net view"; 
    p.StartInfo.RedirectStandardOutput = true; 
    p.Start(); 
    String output = p.StandardOutput.ReadToEnd(); 
    char[] delimiters = new char[] { '\n', '\\' }; 
    string[] s = output.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); 
    string hostName = Dns.GetHostName(); 
    IPHostEntry IPHost = Dns.GetHostEntry(hostName); 
    Console.WriteLine(IPHost.HostName); // Output name of web host 
    IPAddress[] address = IPHost.AddressList; // get list of IP address 
    // Console.WriteLine("List IP {0} :", IPHost.HostName); 
    if (address.Length > 0) 
    { 
     for (int i = 0; i < address.Length; i++) 
     { 
      Console.WriteLine(address[i]); 
     } 
    } 


    p.WaitForExit(); 
    int z = s.Length - 5; 

    string[] str1 = new string[z]; 
    // int i = 0; 
    char[] saperator = { ' ' }; 
    for (int j = 3; j < s.Length - 2; j++) 
    { 
     //Console.WriteLine(s[i]); 
     // str1[i] = (s[j].ToString()).Split(saperator)[0]; 
     // Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString()); 
    } 
    //Console.WriteLine(output); 

    s = output.Split(new string[] { "\n,\\" }, StringSplitOptions.None); 

    //Console.WriteLine(s[i]); 
    //Console.WriteLine(output); 
    // Console.WriteLine("IP Address : {1} ", i, AddressList[i].ToString()); 
    Console.ReadLine(); 

입니다하지만 난 내 컴퓨터의 IP 주소를 얻을.

+0

게시물을 편집하고 코드를 다시 포맷 할 수 있습니까? –

답변

1

호스트 이름을 전달하는 대신 그물보기의 결과를 전달하십시오.

foreach (string hostName in hostNames) 
{ 
    //string hostName = Dns.GetHostName(); 
    IPHostEntry entry = Dns.GetHostEntry(hostName); 
    Console.WriteLine(entry.HostName); // output name of web host 
    IPAddress[] addresses = entry.AddressList; // get list of IP addresses 
    foreach (var address in addresses) 
    { 
     Console.WriteLine(address); 
    } 
} 
관련 문제