通過網絡驗證程序能否運行

@zgcwkj  2018年06月09日

分類:

代碼 其它 

引用文章:HttpHelp網絡請求幫助類說明

修改項目的啟動文件:Program.cs

增加個方法:

/// <summary>
/// 程序服務驗證
/// </summary>
/// <returns></returns>
private static bool ServiceVerificat()
{
    //返回的狀態
    bool status = false;

    try
    {
        new ProxyClass().SwitchOff();
        // HttpGet請求到一個網絡地址,獲取到文本那麼你應該懂了接下來
        string Serve = HttpHelp.HttpGet("網絡地址");
        string[] Serves = Serve.Replace("\r", "").Split('\n');

        #region 更新模塊

        if (Serves.Length >= 1)
        {
            if (Serves[0] != "")
            {
                string[] Update = Serves[0].Split('|');
                //是否強制更新
                if (Convert.ToBoolean(Update[0]))
                {
                    //匹配版本號進行判斷
                    int versions = Convert.ToInt32(Application.ProductVersion.Replace(".", ""));
                    if (versions < Convert.ToInt32(Update[1]))
                    {
                        //提示更新內容
                        MessageBox.Show(Update[2]);
                        //通過瀏覽器跳轉
                        try { System.Diagnostics.Process.Start(Update[3]); } catch { }
                        //直接終止程序運行
                        return false;
                    }
                }
            }
        }

        #endregion 更新模塊

        #region 程序狀態

        if (Serves.Length >= 2)
        {
            if (Serves[1] != "")
            {
                //程序狀態
                if (Convert.ToBoolean(Serves[1])) { status = true; }
            }
        }

        #endregion 程序狀態

        #region 提示內容

        if (Serves.Length >= 3)
        {
            if (Serves[2] != "")
            {
                //提示消息內容
                MessageBox.Show(Serves[2]);
            }
        }

        #endregion 提示內容

        #region 瀏覽器跳轉

        if (Serves.Length >= 4)
        {
            if (Serves[3] != "")
            {
                //通過瀏覽器跳轉
                try { System.Diagnostics.Process.Start(Serves[3]); } catch { }
            }
        }

        #endregion 瀏覽器跳轉
    }
    catch (Exception e)
    {
        string str = e.Message;
        MessageBox.Show("請檢查網絡狀態");
        status = false;
    }
    return status;
}

然後從 程序主入口(Main)中修改為:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (ServiceVerificat())
{
    Application.Run(new 窗口());
}

文章總結:

攔截程序啟動(如果需要每個方法都驗證攔截可以自己想一想如何改)
通過 網絡獲取文本 ,然後通過 文本關鍵字 進行 指定的操作



評論已關閉

Top