C#简单下载类

该博客介绍了一个简单的C#类,用于通过Http协议下载文件。类中包含下载文件、处理下载完成、获取远程文件长度的方法,并提供事件来显示下载进度。

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

通过Http的方式去下载文件

 

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

namespace ConsoleApp1
{
    public class DownLoad
    {

        private static int ByteSize = 1024;

        /// <summary>
        /// 下载中的后缀,下载完成去掉
        /// </summary>
        private  const string Suffix = ".downloading";

        public static event Action<int> ShowDownloadPercent;

        /// <summary>
        /// Http方式下载文件
        /// </summary>
        /// <param name="url">http地址</param>
        /// <param name="localfile">本地文件</param>
        /// <returns></returns>
        public static int DownloadFile(string url, string localfile)
        {
            string savePath = @"C:\Users\wr\Desktop\aaa\"+ localfile;
            string path = @"C:\Users\wr\Desktop\aaa\"+ localfile;

            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Headers["User-Agent"] = "blah";
            byte[] bateFile = wc.DownloadData(url);
            string downLoadPath = string.Empty;
            if (localfile.ToString().ToLower().IndexOf("pdf") == -1)
            {
                downLoadPath = savePath;
            }
            else
            {
                downLoadPath = path;
            }
            System.IO.FileStream fs = new System.IO.FileStream(downLoadPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
            fs.Write(bateFile, 0, bateFile.Length);
            fs.Flush();
            fs.Close();


            return 1;
        }

        /// <summary>
        /// 下载完成
        /// </summary>
        private static void DownloadFileOk(string localfileReal, string localfileWithSuffix)
        {
            try
            {
                //去掉.downloading后缀
                FileInfo fi = new FileInfo(localfileWithSuffix);
                fi.MoveTo(localfileReal);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                //通知完成
                if (ShowDownloadPercent != null)
                {
                    ShowDownloadPercent(100);
                }
            }
        }

        // 从文件头得到远程文件的长度
        private static long GetHttpLength(string url)
        {
            long length = 0;
            HttpWebRequest req = null;
            HttpWebResponse rsp = null;
            try
            {
                req = (HttpWebRequest)HttpWebRequest.Create(url);
                rsp = (HttpWebResponse)req.GetResponse();
                if (rsp.StatusCode == HttpStatusCode.OK)
                {
                    length = rsp.ContentLength;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取远程文件大小失败!exception:\n" + ex.ToString());
            }
            finally
            {
                if (rsp != null)
                {
                    rsp.Close();
                }

                if (req != null)
                {
                    req.Abort();
                }
            }

            return length;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值