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

}

posted @ 2014-08-26 11:17  Seawin龙  阅读(1564)  评论(0编辑  收藏  举报