• c#线程中下载文件到本地


    额,太懒了 直接上示例代码。。。

            /// <summary>
            /// 下载文件到本地   2017-05-31
            /// </summary>
            /// <param name="DownloadPath">本地下载目录</param>
            /// <param name="FullFilePath">下载地址</param>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            private void DownLoadSoft(string DownloadPath, string FullFilePath, string FileName)
            {
                ParameterizedThreadStart s = new ParameterizedThreadStart(DownLoadSoftAu);
                Thread thread = new Thread(s);
                thread.IsBackground = true;
                thread.Start(new object[] { DownloadPath, FullFilePath, FileName });
                thread.Join();
            }
            private void DownLoadSoftAu(object obj)
            {
            
                string DownloadPath = ((object[])obj)[0].ToString().Trim();
                string FullFilePath =((object[])obj)[1].ToString().Trim();
                string FileName = ((object[])obj)[2].ToString().Trim();
                try
                {
                    if (!Directory.Exists(DownloadPath))
                    {
                        Directory.CreateDirectory(DownloadPath);
                    }
                    using (FileStream fs = new FileStream(DownloadPath + FileName, FileMode.Create, FileAccess.Write))
                    {
                        //创建请求
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FullFilePath);
                        //接收响应
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        //输出流
                        Stream responseStream = response.GetResponseStream();
                        byte[] bufferBytes = new byte[10000];//缓冲字节数组
                        int bytesRead = -1;
                        while ((bytesRead = responseStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0)
                        {
                            fs.Write(bufferBytes, 0, bytesRead);
                        }         
                        //关闭写入
                        fs.Flush();
                        fs.Close();
                    }
    
                }
                catch (Exception exp)
                {
                    //返回错误消息
                }
            }
  • 相关阅读:
    python D32 管道、线程池
    python D31 守护进程、进程锁、队列
    python D30 进程
    python 30 进程之间的相互独立、进程之间的时间差
    python D30 操作系统历史
    python D29 socketserver以及FTB
    python D28 粘包
    net4.0 task 超时任务代码 用Thread.sleep方式实现
    sql取随机结果集
    【ecshop---新增包邮卡功能】
  • 原文地址:https://www.cnblogs.com/JLZT1223/p/6927279.html
Copyright © 2020-2023  润新知