向服务器更新文件

//下载版本

复制代码
    /// <summary>
    /// 下载版本
    /// </summary>
    /// <param name="FilePath">服务器文件地址完整</param>
    /// <param name="downLoadPath">下载到本地地址</param>
    /// <returns></returns>
    private bool temDowm(string FilePath ,string downLoadPath) 
    {
        //获取文件名
       string FilePath  = FilePath.Substring(FilePath.LastIndexOf("/") + 1);
        //判断地址是否存在,不存在创建文件
        if (!Directory.Exists(downLoadPath))
        {
            Directory.CreateDirectory(downLoadPath);
        }
        //对文件地址发出请求
        WebRequest request = WebRequest.Create(FilePath);
        WebResponse fs = null;
        try
        {
            fs = request.GetResponse();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "/r/n请确保文件名不存在特殊字符");
            return false;
        }
        long contentLength = fs.ContentLength;

        Stream st = fs.GetResponseStream();
        try
        {
            byte[] byteLength = new byte[contentLength];
            int allByte = (int)contentLength;
            int startByte = 0;
            while (contentLength > 0)
            {
                int downByte = st.Read(byteLength, startByte, allByte);
                if (downByte == 0)
                {
                    break;
                }
                startByte += downByte;
                allByte -= downByte;
            }
            // 打开文件写入文件
            FileStream stream = new FileStream(downLoadPath + @"\" + fileName, FileMode.OpenOrCreate, FileAccess.Write);
            stream.Write(byteLength, 0, byteLength.Length);
            stream.Close();
            fs.Close();
            return true;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }    
    }
复制代码

 

posted @   一招致命九虎山  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
点击右上角即可分享
微信分享提示