如何使用c#实际执行这个ISP拨号器



要连接到互联网,我必须运行我的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了谷歌,并显示了一个相关的消息框。

下面是我目前为止写的代码:
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),但我仍然必须手动按下拨号器中的连接按钮。如何自动设置这个按钮?

如果有人能帮我重新排列代码,我将非常感激。

谢谢。

问候,Shajee a .

我不完全熟悉这个,但我认为你想用rasdial DELTA1而不是rasphone -d DELTA1。Rasdial是为无人值守使用而设计的,而rasphone是为与用户交互而设计的。请看这个问题

如果支持,您可以使用UI自动化API以编程方式单击UI中的按钮。这是一个非常基本的大纲:

using System.Windows.Automation;
Process p = Process.Start("C:\WINDOWS\system32\rasphone.exe","-d DELTA1");
Thread.Sleep(5000); // give the program some time to start.
var windowElement = AutomationElement.FromHandle(p.MainWindowHandle);
var connectButton = windowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Connect"));
var invokePattern = (InvokePattern)connectButton.GetCurrentPattern(InvokePattern.Pattern);
invokePattern.Invoke();

当然,您可能需要一些重试逻辑或错误处理,例如,如果程序在初始化时禁用"连接",然后在稍后启用它。

对于您的代码,您希望在计时器回调中执行ping。我猜你有一个测试按钮。我认为这样更有意义:

private void timer1_Tick(object sender, EventArgs e)
{
    Ping ping = new Ping();
    PingReply pingStatus = ping.Send("www.google.com");
    if (pingStatus.Status != IPStatus.Success)
    {
        Process.Start("C:\WINDOWS\system32\rasphone.exe","-d DELTA1");
        // automation logic here.
    }       
}

最新更新