2010-12-05 103 views
1

我想在C#中編寫一個程序,該程序可以識別現在連接到Internet的計算機,或者不通過C#。 你會幫我怎麼做,我不知道它,因爲我沒有在C#中工作的網絡。查找網絡狀態

還有一個問題,我如何從c#運行程序併發送參數?

+0

回答問題#2:[調試在Visual Studio中的命令行參數(http://stackoverflow.com/q/298708/ 94928) – heavyd 2010-12-05 22:38:04

回答

4

使用微軟的InternetGetConnectedState函數。

你可以用P/Invoke調用它:

using System; 
using System.Runtime.InteropServices; 

namespace ConnectionState 
{ 
    internal class Program 
    { 
     [DllImport("wininet.dll", SetLastError = true)] 
     private static extern bool InternetGetConnectedState(out int lpdwFlags, int dwReserved); 

     private static void Main(string[] args) 
     { 
      int flags; 
      bool isConnected = InternetGetConnectedState(out flags, 0); 
      Console.WriteLine(string.Format("Is connected: {0} Flags:{1}", isConnected, flags)); 
      Console.Read(); 
     } 
    } 
}