C#在線更新程序[下載程序、解壓縮程序、控制台程序]


【1】下載文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace XuanWu.Software.EasyInfo.Interface.Update
{
    public class DownloadThread
    {
        #region DownLoadOneFile(下載單個文件)
        /// <summary>
        /// 下載文件
        /// </summary>
        /// <param name="url">下載地址</param>
        /// <param name="filePath">保存路徑</param>
        /// <returns></returns>
        public bool DownLoadOneFile(string url, string filePath)
        {
            FileStream fstream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            WebRequest wRequest = WebRequest.Create(url);
            try
            {
                WebResponse wResponse = wRequest.GetResponse();
                int contentLength = (int)wResponse.ContentLength;

                byte[] buffer = new byte[Properties.Settings.Default.byte_size];

                ///備注:Properties.Settings.Default.byte_size是從配置文件中讀取的
                int read_count = 0;
                int total_read_count = 0;
                bool complete = false;

                System.Console.WriteLine("開始下載文件....");

                while (!complete)
                {
                    read_count = wResponse.GetResponseStream().Read(buffer, 0, buffer.Length);
                    if (read_count > 0)
                    {
                        fstream.Write(buffer, 0, read_count);
                        total_read_count += read_count;
                        if (total_read_count <= contentLength)
                            System.Console.Write(".");
                    }
                    else
                    {
                        complete = true;
                        System.Console.WriteLine("");
                        System.Console.WriteLine("下載完成!開始安裝!");

                    }
                }
                fstream.Flush();
                return true;
            }
           catch(Exception ex)
            {
                throw ex;
             }
            finally
            {
                fstream.Close();
                wRequest = null;
            }
        }
       #endregion

    }
}

【2】解壓縮文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace XuanWu.Software.EasyInfo.Interface.Update
{
    public class UnZipClass
    {
        private byte[] byffer = new byte[Properties.Settings.Default.byte_size];

         ///備注:Properties.Settings.Default.byte_size是從配置文件中讀取的

        /// <summary>
        /// 有參構造函數
        /// </summary>
        /// <param name="buffserSize">緩沖大小</param>
        public UnZipClass(int buffserSize)
        { 
            byffer = new byte[buffserSize];
        }

        /// <summary>
        /// 無參構造函數
        /// </summary>
        public UnZipClass()
        { }

        /// <summary>
        /// 解壓縮文件
        /// </summary>
        /// <param name="zipFilePath">壓縮文件路徑</param>
        /// <param name="unZipFilePath">解壓縮文件路徑</param>
        public void UnZipFile(string zipFilePath, string unZipFilePath)
        {
            using (ZipInputStream zipstream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry zipEntry = null;
                while ((zipEntry = zipstream.GetNextEntry()) != null)
                {
                    string fileName = Path.GetFileName(zipEntry.Name);
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        if (zipEntry.CompressedSize == 0)
                            break;

                        using (FileStream stream = File.Create(unZipFilePath + fileName))
                        {
                            while (true)
                            {
                                int size = zipstream.Read(byffer, 0, byffer.Length);
                                if (size > 0)
                                    stream.Write(byffer, 0, size);
                                else break;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 解壓縮目錄
        /// </解壓縮目錄summary>
        /// <param name="zipDirectoryPath">壓縮目錄路徑</param>
        /// <param name="unZipDirectoryPath">解壓縮目錄路徑</param>
        public void UnZipDirectory(string zipDirectoryPath, string unZipDirectoryPath)
        { 
            using(ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipDirectoryPath)))
            {
                ZipEntry zipentry = null;
                while ((zipentry = zipStream.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(zipentry.Name);
                    string fileName = Path.GetFileName(zipentry.Name);

                    if (!string.IsNullOrEmpty(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    if (!string.IsNullOrEmpty(fileName))
                    {
                        if (zipentry.CompressedSize == 0)
                            break;
                        if (zipentry.IsDirectory)
                        {
                            directoryName = Path.GetDirectoryName(unZipDirectoryPath + zipentry.Name);
                            Directory.CreateDirectory(directoryName);
                        }

                        using (FileStream stream = File.Create(unZipDirectoryPath + zipentry.Name))
                        {
                            while (true)
                            {
                                int size = zipStream.Read(byffer, 0, byffer.Length);
                                if (size > 0)
                                    stream.Write(byffer, 0, size);
                                else break;
                            }
                        }

                    }
                }
            }
        }

    }
}

 

【3】控制台應用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;
using XuanWu.Software.EasyInfo.Model;
using XuanWu.Software.EasyInfo.Service.elsp_da;

namespace XuanWu.Software.EasyInfo.Interface.Update
{
    class Program
    {
        static void Main(string[] args)
        {

            Protocol.ProtocolRegister.RegisterProcotols();

            string path = "http://" + Properties.Settings.Default.urlServerIP + ":" + Properties.Settings.Default.urlServerPort + "/soft/";

///備注:Properties.Settings.Default.urlServerIP和Properties.Settings.Default.urlServerPort 是從配置文件中讀取的
            string Version = Properties.Settings.Default.Version;   ///版本號
            string buildno = Properties.Settings.Default.buildno;   ///版本狀態
            string clienttypeno = Properties.Settings.Default.clienttypeno;  ///版本類型

///備注:上面3個變量的賦值都是從配置文件中取的。
            try
            {

                ///從本地文件中讀取值,如果沒有存在文件,報錯跳出Try語句.Application.StartupPath + "//Version.bt");

               

                buildno = File.ReadAllText(System.Windows.Forms.Application.StartupPath + "//buildno.bt");

                clienttypeno = File.ReadAllText(System.Windows.Forms.Application.StartupPath + "//clienttypeno.bt");
            }
            catch { }
            updatesoftwareobj updatesfoj = getsoftwareobj(Convert.ToInt32(buildno), Convert.ToInt32(Version), Convert.ToInt32(clienttypeno));

///備注:updatesoftwareobj 實體對象
            if (updatesfoj != null)
            {
                path += updatesfoj.updateurl;
                int versionstatus = updatesfoj.versionstatus;
                string  serverVersion = updatesfoj.versionno.ToString();
                string serverbuildno = updatesfoj.buildno.ToString();
                string serverclienttypeno = updatesfoj.clienttypeno.ToString();
                try
                {
                    if (Convert.ToInt32(serverbuildno)> Convert.ToInt32(buildno))
                    {
                        System.Console.WriteLine("開始升級....");
                        System.Console.WriteLine("————————————————————————");
                        System.Console.WriteLine("開始下載......");
                        DownloadThread down = new DownloadThread();
                        down.DownLoadOneFile(path, System.Windows.Forms.Application.StartupPath + "//updata.zip");
                        System.Console.WriteLine("————————————————————————");
                        System.Console.WriteLine("開始更新......");
                        UnZipClass unzip = new UnZipClass();
                        unzip.UnZipFile(System.Windows.Forms.Application.StartupPath + "//updata.zip", System.Windows.Forms.Application.StartupPath + "//");
                        unzip.UnZipDirectory(System.Windows.Forms.Application.StartupPath + "//updata.zip", System.Windows.Forms.Application.StartupPath + "//");
                        File.Delete(System.Windows.Forms.Application.StartupPath + "//updata.zip");
                        System.Console.WriteLine("更新完成!");
                        System.Console.WriteLine("————————————————————————");
                        System.Console.WriteLine("啟動服務程序!");
                        File.WriteAllText(System.Windows.Forms.Application.StartupPath + "//Version.bt", serverVersion);
                        File.WriteAllText(System.Windows.Forms.Application.StartupPath + "//buildno.bt", serverbuildno);
                        File.WriteAllText(System.Windows.Forms.Application.StartupPath + "//clienttypeno.bt", serverclienttypeno);
                        Process.Start(System.Windows.Forms.Application.StartupPath + "//某服務.exe");
                    }
                }
                catch (Exception ex)
                {
                    File.WriteAllText(System.Windows.Forms.Application.StartupPath + "//installLog.erroe", ex.ToString());
                    System.Console.WriteLine(ex.ToString());
                }
            }
        }

 

        /// <summary>
        ///端版本號及版本號獲取是否有相應的更新信息
        /// </summary>
        /// <param name="buildno"></param>
        /// <param name="versionno"></param>
        /// <param name="clienttypeno"></param>
        /// <returns></returns>
        private static updatesoftwareobj getsoftwareobj(int buildno, int versionno, int clienttypeno)
        {
            DateSourceSystemService datasystesv = new DateSourceSystemService(IPAddress.Parse(Properties.Settings.Default.ServerIP), Properties.Settings.Default.ServerPort);
            updatesoftwareobj updatestobj = datasystesv.getbymodelversion(buildno,versionno,clienttypeno);
            return updatestobj;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM