2014年7月21日 星期一

C#自動撥號程式

適用範圍:VPN撥號、ADSL撥號

前言:
   為何小編我要這樣寫自動撥號系統呢?原因就在於希望VPN可以自動管理不需要使用人工方式去管理,而小編各人也自己開了CCPROXY的伺服器去調用VPN的網址如果一直使用人工管理不僅麻煩還要適時後注意,傷神又傷己

前言說明完畢,開始進入正式的使用方法

用法:

我們會調用Windows DLL中的 wininet.dll檔案

規則用法如下:
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

編寫副程式呼叫方法:

--------------------------------------------
        public void IsConnectedToInternet()
        {
            InternetGetConnectedState(out Desc, 0);
        }
        int Desc;
        private Process dailer = new Process();
        private static Mutex mutex = new Mutex();

        ///
        /// ADSL 拔号
        ///
        /// ADSL名称
        /// 用户
        /// 密码
        public bool Connection(string name, string user, string pass)
        {
            bool IsSuccess = false;

            string args = string.Concat(name, " ", user, " ", pass);

            while (Desc != 81)
            {
                lock (dailer)
                {
                    if (!IsAlive("rasdial"))
                    {
                        mutex.WaitOne();
                        dailer.StartInfo.FileName = "rasdial.exe";
                        dailer.StartInfo.Arguments = args;
                        dailer.StartInfo.UseShellExecute = false;
                        dailer.StartInfo.CreateNoWindow = true;
                        dailer.StartInfo.RedirectStandardInput = true;
                        dailer.StartInfo.RedirectStandardOutput = true;
                        dailer.StartInfo.RedirectStandardError = true;
                        dailer.Start();
                        string strOutput = dailer.StandardOutput.ReadToEnd();

                        mutex.ReleaseMutex();
                    }
                }
                IsConnectedToInternet();
                IsSuccess = true;
            }
            dailer.Close();
            Desc = 0;
            return IsSuccess;
        }

        ///
        /// 断开ADSL
        ///
        public void Disconnect()
        {
            Process dailer = new Process();
            dailer.StartInfo.FileName = "rasdial.exe";
            dailer.StartInfo.Arguments = "/DISCONNECT";
            dailer.StartInfo.UseShellExecute = false;
            dailer.StartInfo.CreateNoWindow = true;
            dailer.StartInfo.RedirectStandardInput = true;
            dailer.StartInfo.RedirectStandardOutput = true;
            dailer.StartInfo.RedirectStandardError = true;
            dailer.Start();
            string strOutput = dailer.StandardOutput.ReadToEnd();
            dailer.Close();
        }
        ///
        /// 判断rasdial.exe进程
        ///
        ///
        ///
        private bool IsAlive(string name)
        {
            Process[] ps = Process.GetProcessesByName(name);
            if (ps.Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
-------------------------------------------------------------------------

調用方法:
        private void Form1_Load(object sender, System.EventArgs e)
        {
            Connection("連線名稱", "連線帳號", "連線密碼");
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Disconnect();
        }

測試結果:

可以使用

沒有留言:

張貼留言