一个简单的控制ADSL拨号的小程序

来源:互联网 发布:豆瓣电台 mac 编辑:程序博客网 时间:2024/06/11 09:25

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

        
//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

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

}