I want to write a program in C# that recognize now computer connected to internet or not by C#. Would you help me how to do that, I have no idea about it,because I didnt work network in C#.
one more question,ho开发者_如何学Pythonw I can run a program from c# and sent argument also?
Use Microsoft's InternetGetConnectedState function.
You can call it with 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();
}
}
}
精彩评论