Form1.cs: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.IO;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using Microsoft.Win32;using System.Diagnostics;using System.Threading;namespace Crazy7_ADSLApp...{ public partial class C7Adsl : Form ...{ Reg&UnReg HotKey#region Reg&UnReg HotKey [DllImport("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk); [DllImport("user32.dll", SetLastError = true)] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); [Flags()] public enum KeyModifiers ...{ None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } private void C7Adsl_Load(object sender, EventArgs e) ...{ RegisterHotKey(Handle, 7, KeyModifiers.Windows, Keys.F2); RegisterHotKey(Handle, 77, KeyModifiers.Windows, Keys.F3); GetConnectionName(); string txtPath = "C:/time.txt"; if (File.Exists(txtPath)) ...{ tbTotalTime.Text = File.ReadAllText(txtPath); } } private void C7Adsl_FormClosing(object sender, FormClosingEventArgs e) ...{ if (IsConnect()) ...{ SaveTime(); } UnregisterHotKey(Handle, 77); UnregisterHotKey(Handle, 7); } protected override void WndProc(ref Message m) ...{ const int WM_HOTKEY = 0x312; switch (m.Msg) ...{ case (WM_HOTKEY): ...{ if (m.WParam.ToInt32() == 7) ...{ AdslOperation(cbConnection.SelectedItem.ToString(), true); this.Hide(); } if (m.WParam.ToInt32() == 77) ...{ this.Show(); if ((count % 60) > 57) ...{ timer.Stop(); } else ...{ if (IsConnect()) ...{ int RemainTime = 57 - (count % 60); MessageBox.Show("连接将在" + RemainTime + "秒之后关闭!"); Thread.Sleep(RemainTime * 1000); AdslOperation(cbConnection.SelectedItem.ToString(), false); SaveTime(); } } } break; } } base.WndProc(ref m); } #endregion //use to judge whether already connect to Internet [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int Description, int ReservedValue); public C7Adsl() ...{ InitializeComponent(); } string time; private void SaveTime() ...{ string txtPath = "C:/time.txt"; if (File.Exists(txtPath)) ...{ time = File.ReadAllText(txtPath); //Read old int CurrentTime = count / 60 + 1; //Get the time of this time. int SaveTime = CurrentTime + Int32.Parse(time.Trim()); using (StreamWriter txtW = new StreamWriter(txtPath)) ...{ txtW.WriteLine(SaveTime); MessageBox.Show("本次连接时间为" + CurrentTime.ToString() + "分钟"); } } else ...{ using (StreamWriter txtW = new StreamWriter(txtPath)) ...{ txtW.WriteLine(Convert.ToString(count / 60 + 1)); //if no old record } } } private bool IsConnect() ...{ int Description; return InternetGetConnectedState(out Description, 0); } protected void GetConnectionName() ...{ try ...{ RegistryKey UserKey = Registry.CurrentUser; RegistryKey Key = UserKey.OpenSubKey("RemoteAccess/Profile"); string[] KeysList = Key.GetSubKeyNames(); foreach (string key in KeysList) ...{ cbConnection.Items.Add(key); } cbConnection.SelectedIndex = 0; } catch (Exception ex) ...{ MessageBox.Show(ex.Message); } } protected void AdslOperation(string Connection, bool Instruction) ...{ string WinDir = Environment.GetFolderPath(Environment.SpecialFolder.System); string file = "/rasphone.exe "; string args=""; if(Instruction == true) ...{ args = "-d " + """ + Connection + """; timer.Start(); } if (Instruction == false) ...{ args = "-h " + """ + Connection + """; timer.Stop(); } string FileName = WinDir + file; ProcessStartInfo AdslOp = new ProcessStartInfo(); AdslOp.FileName = FileName; AdslOp.Arguments = args; Process.Start(AdslOp); } //Connect & Disconnect private void bnConnect_Click(object sender, EventArgs e) ...{ AdslOperation(cbConnection.SelectedItem.ToString(), true); } private void bnDisConnection_Click(object sender, EventArgs e) ...{ if (IsConnect()) ...{ SaveTime(); AdslOperation(cbConnection.SelectedItem.ToString(), false); } } int count=0; private void timer_Tick(object sender, EventArgs e) ...{ count++; } //begin take count of "passed time" private void bnClearRecord_Click(object sender, EventArgs e) ...{ string txtPath = "C:/time.txt"; if (File.Exists(txtPath)) ...{ using (StreamWriter txtW = new StreamWriter(txtPath)) ...{ txtW.WriteLine("0"); } } MessageBox.Show("OK!"); tbTotalTime.Text = File.ReadAllText(txtPath); } }} Form1.Designer.cs: namespace Crazy7_ADSLApp...{ partial class C7Adsl ...{ /**//// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /**//// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) ...{ if (disposing && (components != null)) ...{ components.Dispose(); } base.Dispose(disposing); } Windows Form Designer generated code#region Windows Form Designer generated code /**//// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() ...{ this.components = new System.ComponentModel.Container(); this.cbConnection = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.bnConnect = new System.Windows.Forms.Button(); this.bnDisConnection = new System.Windows.Forms.Button(); this.timer = new System.Windows.Forms.Timer(this.components); this.tbTotalTime = new System.Windows.Forms.TextBox(); this.bnClearRecord = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // cbConnection // this.cbConnection.FormattingEnabled = true; this.cbConnection.Location = new System.Drawing.Point(43, 25); this.cbConnection.Name = "cbConnection"; this.cbConnection.Size = new System.Drawing.Size(155, 21); this.cbConnection.TabIndex = 0; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 9); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(95, 13); this.label1.TabIndex = 1; this.label1.Text = "请选择拨号连接:"; // // bnConnect // this.bnConnect.Location = new System.Drawing.Point(32, 62); this.bnConnect.Name = "bnConnect"; this.bnConnect.Size = new System.Drawing.Size(75, 23); this.bnConnect.TabIndex = 2; this.bnConnect.Text = "连接"; this.bnConnect.UseVisualStyleBackColor = true; this.bnConnect.Click += new System.EventHandler(this.bnConnect_Click); // // bnDisConnection // this.bnDisConnection.Location = new System.Drawing.Point(123, 62); this.bnDisConnection.Name = "bnDisConnection"; this.bnDisConnection.Size = new System.Drawing.Size(75, 23); this.bnDisConnection.TabIndex = 3; this.bnDisConnection.Text = "断开"; this.bnDisConnection.UseVisualStyleBackColor = true; this.bnDisConnection.Click += new System.EventHandler(this.bnDisConnection_Click); // // timer // this.timer.Interval = 1000; this.timer.Tick += new System.EventHandler(this.timer_Tick); // // tbTotalTime // this.tbTotalTime.Location = new System.Drawing.Point(133, 98); this.tbTotalTime.Name = "tbTotalTime"; this.tbTotalTime.Size = new System.Drawing.Size(65, 23); this.tbTotalTime.TabIndex = 4; // // bnClearRecord // this.bnClearRecord.Location = new System.Drawing.Point(85, 137); this.bnClearRecord.Name = "bnClearRecord"; this.bnClearRecord.Size = new System.Drawing.Size(65, 23); this.bnClearRecord.TabIndex = 5; this.bnClearRecord.Text = "清零"; this.bnClearRecord.UseVisualStyleBackColor = true; this.bnClearRecord.Click += new System.EventHandler(this.bnClearRecord_Click); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(16, 101); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(111, 13); this.label2.TabIndex = 6; this.label2.Text = "已使用时间(Min):"; // // C7Adsl // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(228, 177); this.Controls.Add(this.label2); this.Controls.Add(this.bnClearRecord); this.Controls.Add(this.tbTotalTime); this.Controls.Add(this.bnDisConnection); this.Controls.Add(this.bnConnect); this.Controls.Add(this.label1); this.Controls.Add(this.cbConnection); this.Name = "C7Adsl"; this.Text = "Crazy7's App 4 Adsl"; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.C7Adsl_FormClosing); this.Load += new System.EventHandler(this.C7Adsl_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.ComboBox cbConnection; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button bnConnect; private System.Windows.Forms.Button bnDisConnection; private System.Windows.Forms.Timer timer; private System.Windows.Forms.TextBox tbTotalTime; private System.Windows.Forms.Button bnClearRecord; private System.Windows.Forms.Label label2; }}