c# 软件自动在线更新代码(二)

        /// <summary> 
        /// 获取文件列表并下载 
        /// </summary> 
        private static void UpdateList() 

        /// <summary> 
        /// 获取文件列表并下载 
        /// </summary> 
        private static void UpdateList() 
        { 
            string xmlPath = dirPath + "AutoUpdater/AutoUpdater.xml"; 
            WebClient wc = new WebClient(); 
            DataSet ds = new DataSet(); 
            ds.Locale = CultureInfo.InvariantCulture; 
             
            try 
            { 
                Stream sm = wc.OpenRead(xmlPath); 
                ds.ReadXml(sm); 
                DataTable dt = ds.Tables["UpdateFileList"]; 
                StringBuilder sb = new StringBuilder(); 
                count = dt.Rows.Count; 
                for (int i = 0; i < dt.Rows.Count; i++) 
                { 
                    if (i == 0) 
                    { 
                        sb.Append(dt.Rows[i]["UpdateFile"].ToString()); 
                    } 
                    else 
                    { 
                        sb.Append("," + dt.Rows[i]["UpdateFile"].ToString()); 
                    } 
                } 
                fileNames = sb.ToString().Split(','); 
                sm.Close(); 
            } 
            catch (WebException ex) 
            { 
                MeBox(ex.Message); 
            } 
        }

        /// <summary> 
        /// 下载文件 
        /// </summary> 
        /// <param name="arry">下载序号</param> 
        private void DownloadFile(int arry) 
        { 
            try 
            { 
                num++; 
                fileName = fileNames[arry]; 
                this.label1.Text = String.Format( 
                    CultureInfo.InvariantCulture, 
                    "更新进度 {0}/{1} [ {2} ]", 
                    num, 
                    count, 
                    ConvertSize(size));

                this.progressBar2.Value = 0; 
                this.downWebClient.DownloadFileAsync( 
                    new Uri(dirPath + "AutoUpdater/" + fileName), 
                    Application.StartupPath + "\\AutoUpdater\\" + fileName); 
            } 
            catch (WebException ex) 
            { 
                MeBox(ex.Message); 
            } 
        }

        /// <summary> 
        /// 转换字节大小 
        /// </summary> 
        /// <param name="byteSize">输入字节数</param> 
        /// <returns>返回值</returns> 
        private static string ConvertSize(long byteSize) 
        { 
            string str = ""; 
            float tempf = (float)byteSize; 
            if (tempf / 1024 > 1) 
            { 
                if ((tempf / 1024) / 1024 > 1) 
                { 
                    str = ((tempf / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB"; 
                } 
                else 
                { 
                    str = (tempf / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB"; 
                } 
            } 
            else 
            { 
                str = tempf.ToString(CultureInfo.InvariantCulture) + "B"; 
            } 
            return str; 
        }

        /// <summary> 
        /// 弹出提示框 
        /// </summary> 
        /// <param name="txt">输入提示信息</param> 
        private static void MeBox(string txt) 
        { 
            MessageBox.Show( 
                txt, 
                "提示信息", 
                MessageBoxButtons.OK, 
                MessageBoxIcon.Asterisk, 
                MessageBoxDefaultButton.Button1, 
                MessageBoxOptions.DefaultDesktopOnly); 
        }

        /// <summary> 
        /// 关闭程序 
        /// </summary> 
        private static void UpdaterClose() 
        { 
            try 
            { 
                System.Diagnostics.Process.Start(Application.StartupPath + "\\ComCir.exe"); 
            } 
            catch (Win32Exception ex) 
            { 
                MeBox(ex.Message); 
            } 
            Application.Exit(); 
        }

        /// <summary> 
        /// 读取.exe.config的值 
        /// </summary> 
        /// <param name="path">.exe.config文件的路径</param> 
        /// <param name="appKey">"key"的值</param> 
        /// <returns>返回"value"的值</returns> 
        internal static string GetConfigValue(string path, string appKey) 
        { 
            XmlDocument xDoc = new XmlDocument(); 
            XmlNode xNode; 
            XmlElement xElem = null; 
            try 
            { 
                xDoc.Load(path);

                xNode = xDoc.SelectSingleNode("//appSettings");

                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); 
                 
            } 
            catch (XmlException ex) 
            { 
                MeBox(ex.Message); 
            } 
            if (xElem != null) 
                    return xElem.GetAttribute("value"); 
                else 
                    return ""; 
        }

        /// <summary> 
        /// 设置.exe.config的值 
        /// </summary> 
        /// <param name="path">.exe.config文件的路径</param> 
        /// <param name="appKey">"key"的值</param> 
        /// <param name="appValue">"value"的值</param> 
        internal static void SetConfigValue(string path, string appKey, string appValue) 
        { 
            XmlDocument xDoc = new XmlDocument(); 
            try 
            { 
                xDoc.Load(path);

                XmlNode xNode; 
                XmlElement xElem1; 
                XmlElement xElem2;

                xNode = xDoc.SelectSingleNode("//appSettings");

                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); 
                if (xElem1 != null) xElem1.SetAttribute("value", appValue); 
                else 
                { 
                    xElem2 = xDoc.CreateElement("add"); 
                    xElem2.SetAttribute("key", appKey); 
                    xElem2.SetAttribute("value", appValue); 
                    xNode.AppendChild(xElem2); 
                } 
                xDoc.Save(Application.StartupPath + "\\" + path); 
            } 
            catch (XmlException ex) 
            { 
                MeBox(ex.Message); 
            } 
        }

        /// <summary> 
        /// 判断软件的更新日期 
        /// </summary> 
        /// <param name="Dir">服务器地址</param> 
        /// <returns>返回日期</returns> 
        private static string GetTheLastUpdateTime(string Dir) 
        { 
            string LastUpdateTime = ""; 
            string AutoUpdaterFileName = Dir + "AutoUpdater/AutoUpdater.xml"; 
            try 
            { 
                WebClient wc = new WebClient(); 
                Stream sm = wc.OpenRead(AutoUpdaterFileName); 
                XmlTextReader xml = new XmlTextReader(sm); 
                while (xml.Read()) 
                { 
                    if (xml.Name == "UpdateTime") 
                    { 
                        LastUpdateTime = xml.GetAttribute("Date"); 
                        break; 
                    } 
                } 
                xml.Close(); 
                sm.Close(); 
            } 
            catch (WebException ex) 
            { 
                MeBox(ex.Message); 
            } 
            return LastUpdateTime; 
        } 
    } 
}

        { 
            string xmlPath = dirPath + "AutoUpdater/AutoUpdater.xml"; 
            WebClient wc = new WebClient(); 
            DataSet ds = new DataSet(); 
            ds.Locale = CultureInfo.InvariantCulture; 
             
            try 
            { 
                Stream sm = wc.OpenRead(xmlPath); 
                ds.ReadXml(sm); 
                DataTable dt = ds.Tables["UpdateFileList"]; 
                StringBuilder sb = new StringBuilder(); 
                count = dt.Rows.Count; 
                for (int i = 0; i < dt.Rows.Count; i++) 
                { 
                    if (i == 0) 
                    { 
                        sb.Append(dt.Rows[i]["UpdateFile"].ToString()); 
                    } 
                    else 
                    { 
                        sb.Append("," + dt.Rows[i]["UpdateFile"].ToString()); 
                    } 
                } 
                fileNames = sb.ToString().Split(','); 
                sm.Close(); 
            } 
            catch (WebException ex) 
            { 
                MeBox(ex.Message); 
            } 
        }

        /// <summary> 
        /// 下载文件 
        /// </summary> 
        /// <param name="arry">下载序号</param> 
        private void DownloadFile(int arry) 
        { 
            try 
            { 
                num++; 
                fileName = fileNames[arry]; 
                this.label1.Text = String.Format( 
                    CultureInfo.InvariantCulture, 
                    "更新进度 {0}/{1} [ {2} ]", 
                    num, 
                    count, 
                    ConvertSize(size));

                this.progressBar2.Value = 0; 
                this.downWebClient.DownloadFileAsync( 
                    new Uri(dirPath + "AutoUpdater/" + fileName), 
                    Application.StartupPath + "\\AutoUpdater\\" + fileName); 
            } 
            catch (WebException ex) 
            { 
                MeBox(ex.Message); 
            } 
        }

        /// <summary> 
        /// 转换字节大小 
        /// </summary> 
        /// <param name="byteSize">输入字节数</param> 
        /// <returns>返回值</returns> 
        private static string ConvertSize(long byteSize) 
        { 
            string str = ""; 
            float tempf = (float)byteSize; 
            if (tempf / 1024 > 1) 
            { 
                if ((tempf / 1024) / 1024 > 1) 
                { 
                    str = ((tempf / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB"; 
                } 
                else 
                { 
                    str = (tempf / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB"; 
                } 
            } 
            else 
            { 
                str = tempf.ToString(CultureInfo.InvariantCulture) + "B"; 
            } 
            return str; 
        }

        /// <summary> 
        /// 弹出提示框 
        /// </summary> 
        /// <param name="txt">输入提示信息</param> 
        private static void MeBox(string txt) 
        { 
            MessageBox.Show( 
                txt, 
                "提示信息", 
                MessageBoxButtons.OK, 
                MessageBoxIcon.Asterisk, 
                MessageBoxDefaultButton.Button1, 
                MessageBoxOptions.DefaultDesktopOnly); 
        }

        /// <summary> 
        /// 关闭程序 
        /// </summary> 
        private static void UpdaterClose() 
        { 
            try 
            { 
                System.Diagnostics.Process.Start(Application.StartupPath + "\\ComCir.exe"); 
            } 
            catch (Win32Exception ex) 
            { 
                MeBox(ex.Message); 
            } 
            Application.Exit(); 
        }

        /// <summary> 
        /// 读取.exe.config的值 
        /// </summary> 
        /// <param name="path">.exe.config文件的路径</param> 
        /// <param name="appKey">"key"的值</param> 
        /// <returns>返回"value"的值</returns> 
        internal static string GetConfigValue(string path, string appKey) 
        { 
            XmlDocument xDoc = new XmlDocument(); 
            XmlNode xNode; 
            XmlElement xElem = null; 
            try 
            { 
                xDoc.Load(path);

                xNode = xDoc.SelectSingleNode("//appSettings");

                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); 
                 
            } 
            catch (XmlException ex) 
            { 
                MeBox(ex.Message); 
            } 
            if (xElem != null) 
                    return xElem.GetAttribute("value"); 
                else 
                    return ""; 
        }

        /// <summary> 
        /// 设置.exe.config的值 
        /// </summary> 
        /// <param name="path">.exe.config文件的路径</param> 
        /// <param name="appKey">"key"的值</param> 
        /// <param name="appValue">"value"的值</param> 
        internal static void SetConfigValue(string path, string appKey, string appValue) 
        { 
            XmlDocument xDoc = new XmlDocument(); 
            try 
            { 
                xDoc.Load(path);

                XmlNode xNode; 
                XmlElement xElem1; 
                XmlElement xElem2;

                xNode = xDoc.SelectSingleNode("//appSettings");

                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); 
                if (xElem1 != null) xElem1.SetAttribute("value", appValue); 
                else 
                { 
                    xElem2 = xDoc.CreateElement("add"); 
                    xElem2.SetAttribute("key", appKey); 
                    xElem2.SetAttribute("value", appValue); 
                    xNode.AppendChild(xElem2); 
                } 
                xDoc.Save(Application.StartupPath + "\\" + path); 
            } 
            catch (XmlException ex) 
            { 
                MeBox(ex.Message); 
            } 
        }

        /// <summary> 
        /// 判断软件的更新日期 
        /// </summary> 
        /// <param name="Dir">服务器地址</param> 
        /// <returns>返回日期</returns> 
        private static string GetTheLastUpdateTime(string Dir) 
        { 
            string LastUpdateTime = ""; 
            string AutoUpdaterFileName = Dir + "AutoUpdater/AutoUpdater.xml"; 
            try 
            { 
                WebClient wc = new WebClient(); 
                Stream sm = wc.OpenRead(AutoUpdaterFileName); 
                XmlTextReader xml = new XmlTextReader(sm); 
                while (xml.Read()) 
                { 
                    if (xml.Name == "UpdateTime") 
                    { 
                        LastUpdateTime = xml.GetAttribute("Date"); 
                        break; 
                    } 
                } 
                xml.Close(); 
                sm.Close(); 
            } 
            catch (WebException ex) 
            { 
                MeBox(ex.Message); 
            } 
            return LastUpdateTime; 
        } 
    } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值