我想用 C# 编写一个程序,它可以识别现在是否通过 C# 连接到互联网的计算机。 你能帮我怎么做吗,我不知道,因为我没有在 C# 中工作。
还有一个问题,如何从 C# 运行程序并发送参数?
请您参考如下方法:
使用微软的 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();
}
}
}
