• 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)
                {
                    //返回错误消息
                }
            }
  • 相关阅读:
    好用的jsDelivr
    纯css实现图片或者页面变灰色
    三元表达式的连写
    markdown常用语法小结
    vue中的事件修饰符
    ES6笔记一
    less使用小结
    flex布局小结
    所思所想,关于坚持
    前端渲染图片报403问题解决方案
  • 原文地址:https://www.cnblogs.com/JLZT1223/p/6927279.html
走看看 - 开发者的网上家园