要连接到互联网,我必须运行我的 ISP 提供的拨号器。现在的问题是,每当我离开我的电脑过夜(用于下载目的),拨号器断开连接(网络故障或只是随机断开连接)并且下载停止。这对我来说非常令人沮丧。
所以我尝试编写一个 C# 程序,它将:
1. When you run it, it will start 30 minutes timer.
2. After 30 minutes, it will ping google to check if the net is connected or not.
3. If it is connected, no action will be performed and timer will again check after 30 minutes.
4. If it is not connected then it will execute my ISP's dailer. After connecting, it will again check after 30 minutes.
现在的问题是我对 C# 非常陌生,并且不能很好地编码。但我成功地 ping 了 google 并显示了一个相关的消息框。
这是我到目前为止编写的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;
using System.Diagnostics;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
Timer timer = new Timer();
public Form1()
{
InitializeComponent();
timer.Tick += new EventHandler(timer1_Tick);
timer.Interval = (1000) * (10);
timer.Enabled = true;
timer.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Ping ping = new Ping();
PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"));
if (pingStatus.Status == IPStatus.Success)
{
MessageBox.Show("Pinged Google Successfully");
}
else
{
MessageBox.Show("Can't Ping to Google.");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Process.Start("C:\\WINDOWS\\system32\\rasphone.exe","-d DELTA1");
}
}
}
现在我知道这段代码有点乱,但它最终确实打开了我的 ISP 拨号器(图片:http ://s12.postimg.org/xscgsdcxp/untitled.jpg ://s12.postimg.org/xscgsdcxp/untitled.jpg )但我仍然必须手动按下拨号器中的连接按钮。如何自动化此按钮?
另外,如果有人可以帮助我重新排列代码,我将不胜感激。
谢谢你。
问候, Shajee A.