Conris.Utility/Files.Save.cs

(开头部分) 44KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

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

namespace Conris.Utility.Files
{
    public class Save
    {
        /// <summary>
        /// 下载文件到指定目录,并返回下载后存放的文件路径
        /// </summary>
        /// <param name="Uri">网址</param>
        /// <param name="存放目录">存放目录,如果该目录中已存在与待下载文件同名的文件,那么将自动重命名</param>
        /// <returns>下载文件存放的文件路径</returns>
        public static string DownloadFile(Uri Uri, string SavePath)
        {
            var q = WebRequest.Create(Uri).GetResponse();
            var s = q.GetResponseStream();
            var b = new BinaryReader(s);
            var file = GetDownloadFileSavePath(SavePath, Uri, q.ContentType);
            FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write);
            fs.Write(b.ReadBytes((int)q.ContentLength), 0, (int)q.ContentLength);
            fs.Close();
            b.Close();
            s.Close();
            return file;
        }

        private static string GetDownloadFileSavePath(string SavePath, Uri Uri, string ContentType)
        {
            var ex = GetExtName(ContentType);
            string up = null;
            string upne = null;
            if (Uri.LocalPath == "/")
            {
                //处理Url是域名的情况
                up = upne = Uri.Host;
            }
            else
            {
                if (Uri.LocalPath.EndsWith("/"))
                {
                    //处理Url是目录的情况
                    up = Uri.LocalPath.Substring(0, Uri.LocalPath.Length - 1);
                    upne = Path.GetFileName(up);
                }
                else
                {
                    //处理常规Url
                    up = Uri.LocalPath;
                    upne = Path.GetFileNameWithoutExtension(up);
                }
            }
            var name = string.IsNullOrEmpty(ex) ? Path.GetFileName(up) : upne + "." + ex;
            var fn = Path.Combine(SavePath, name);
            var x = 1;
            while (File.Exists(fn))
后面还有 433 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。