吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1236|回复: 7
收起左侧

[求助] C#下载一个网页上的小文件

  [复制链接]
遗憾迟香 发表于 2020-1-21 16:06
由于种种原因不想再用易语言开发软件了,现在试着使用c#编写一款音乐下载软件
下载文件用易语言是这样写的:写到文件 (目录, 到字节集 (网页_访问_对象 (下载地址)))
想着用c#应该是System.IO中的File.Create或File.Copy方法
结果都没有做出来
另外易语言中的网页_访问_对象不知道用c#怎么写
求助大神
TIM截图20200121160206.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

德古拉伯 发表于 2020-1-21 16:08
[C#] 纯文本查看 复制代码
    public partial class Form1 : Form
    {
        string str;
 
        public string Str
        {
            get { return str; }
            set { str = value; }
        }
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] index;
          index=System.IO.Directory.GetFiles(@"网站的路径","*.*",System.IO.SearchOption.AllDirectories);
          for (int i = 0; i < index.Length; i++) {
              if (index[i].Length > 2300)
              {
                  Str += index[i];
                  //Str += "<br>";
              }
           
          }
          label1.Text = Str;
        }
    }
德古拉伯 发表于 2020-1-21 16:10
或者用第三方库,https://github.com/UweKeim/ZetaLongPaths
coolcalf 发表于 2020-1-21 16:38
本帖最后由 coolcalf 于 2020-1-21 16:40 编辑

/// <summary>        
        /// c#,.net 下载文件        
        /// </summary>        
        /// <param name="URL">下载文件地址</param>      
        ///
        /// <param name="Filename">下载后的存放地址</param>        
        /// <param name="Prog">用于显示的进度条</param>        
        ///
        public static void DownloadFile(string URL, string filename, System.Windows.Controls.ProgressBar prog, System.Windows.Controls.Label label1)
        {
            float percent = 0;
            try
            {
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
                if (prog != null)
                {
                    prog.Maximum = (int)totalBytes;
                }
                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    //System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);
                    if (prog != null)
                    {
                        prog.Value = (int)totalDownloadedByte;
                    }
                    osize = st.Read(by, 0, (int)by.Length);

                    percent = (float)totalDownloadedByte / (float)totalBytes * 100;
                    label1.Content = "当前补丁下载进度" + percent.ToString() + "%";
                    //System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
                }
                so.Close();
                st.Close();
            }
            catch (System.Exception)
            {
                throw;
            }
        }



需要引用:
PresentationCore
PresentationFramework
WindowsBase

因为是一个实例中的代码,略有点乱,供参考
ldgreg1 发表于 2020-1-21 16:41
[C#] 纯文本查看 复制代码
        #region 简单GET方式请求 +GetUrlHtml(string url, Encoding readCoding, int timeout = 30000,  string userAgent = "", CookieCollection cookies = null)
        /// <summary>
        /// 简单GET方式请求 +GetUrlHtml(string url, Encoding readCoding, int timeout = 30000,  string userAgent = "", CookieCollection cookies = null)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="readCoding"></param>
        /// <param name="timeout"></param>
        /// <param name="userAgent"></param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public static string GetUrlHtml(string url, Encoding readCoding, int timeout = 30000, string userAgent = "", CookieCollection cookies = null)
        {
            string content = "";
            HttpWebResponse myResponse;
            try
            {
                myResponse = CreateGetHttpResponse(url, timeout, userAgent, cookies);
            }
            catch (WebException ex)
            {
                myResponse = (HttpWebResponse)ex.Response;
            }

            if (myResponse != null)
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), readCoding);
                content = reader.ReadToEnd();
            }
            return content;
        }
        #endregion


不能直接用IO吧,这是我写的下载网页的。。  最后的  StreamReader 你简单改一下,存成别的类型也行。
jefel 发表于 2020-1-21 17:02
真是学以至用!
414269678 发表于 2020-1-21 17:54
System.IO是读取系统文件的
网站的网址,需要用http的库下载后再保存到电脑上,用HttpClient或者其他的库都行
304775988 发表于 2020-1-21 18:41
看来楼主用易语言纠结了好一段时间,决定重回经典
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-18 11:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表