开发者

find network status

开发者 https://www.devze.com 2023-01-29 12:39 出处:网络
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#.

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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消