C#控制台程序自动重启(检测是否连接网络)
1、添加System.Windows.Forms的引用。
2、在命名空间上using一下。
3、要实现的部分:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ThreadTest
{
class Program
{
[DllImport("winInet.dll ")]
//声明外部的函数:
private static extern bool InternetGetConnectedState(
ref int dwFlag,
int dwReserved
);
static void Main(string[] args)
{
int dwFlag = 0;
if (!InternetGetConnectedState(ref dwFlag, 0))
Console.WriteLine("未联网!");
else
{
Console.WriteLine("联网!");
Thread.Sleep(2000);
Application.Restart();
Environment.Exit(0);
}
Console.ReadKey();
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步