【C#】自动更新

这篇博客介绍了如何使用C#实现客户端程序的自动更新,通过FTP模式下载文件并直接替换本地程序,无需版本比对。内容包括更新窗体WaittingForm的设计,根据操作系统位数判断以及文件下载和替换的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端程序自动更新

客户端程序更新,通常做法是做一个更新程序通过TCP/IP,HTTP,FTP协议指定路径,下载文件与本地文件进行比对。比对成功之后下载压缩包并解压替换本地程序。
本文暂不比对版本,直接从FTP目录下载文件替换本地程序。

本次采用FTP模式

FtpHelper.cs

    static public class FtpHelper
    {
   
        //基本设置
        static private string path = @"ftp://" + Helper.GetAppConfig("obj") + "/";    //目标路径
        static private string ftpip = Helper.GetAppConfig("obj");    //ftp IP地址
        static private string username = Helper.GetAppConfig("username");   //ftp用户名
        static private string password = Helper.GetAppConfig("password");   //ftp密码

        //获取ftp上面的文件和文件夹
        public static string[] GetFileList(string dir)
        {
   
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest request;
            try
            {
   
                request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
                request.UseBinary = true;
                request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.UseBinary = true;

                WebResponse response = request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());

                string line = reader.ReadLine();
                while (line != null)
                {
   
                    result.Append(line);
                    result.Append("\n");
                    Console.WriteLine(line);
                    line = reader.ReadLine();
                }
                // to remove the trailing '\n'
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
   
                Console.WriteLine("获取ftp上面的文件和文件夹:" + ex.Message);
                downloadFiles = null;
                return downloadFiles;
            }
        }

        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="file">ip服务器下的相对路径</param>
        /// <returns>文件大小</returns>
        public static int GetFileSize(string file,string Dir)
        {
   
            StringBuilder result = new StringBuilder();
            FtpWebRequest request;
            try
            {
   
                request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path+ "\\" + Dir + "\\" + file));
                request.UseBinary = true;
                request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
                request.Method = WebRequestMethods.Ftp.GetFileSize;

                int dataLength = (int)request.GetResponse().ContentLength;

                return dataLength;
            }
            catch (Exception ex)
            {
   
                Console.WriteLine("获取文件大小出错:" + ex.Message);
                return -1;
            }
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="filePath">原路径(绝对路径)包括文件名</param>
        /// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param>
        public static void FileUpLoad(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门德仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值